clang 10.0.0git

DataCollection.h
Go to the documentation of this file.
1 //===--- DatatCollection.h --------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 /// This file declares helper methods for collecting data from AST nodes.
10 ///
11 /// To collect data from Stmt nodes, subclass ConstStmtVisitor and include
12 /// StmtDataCollectors.inc after defining the macros that you need. This
13 /// provides data collection implementations for most Stmt kinds. Note
14 /// that the code requires some conditions to be met:
15 ///
16 /// - There must be a method addData(const T &Data) that accepts strings,
17 /// integral types as well as QualType. All data is forwarded using
18 /// to this method.
19 /// - The ASTContext of the Stmt must be accessible by the name Context.
20 ///
21 /// It is also possible to override individual visit methods. Have a look at
22 /// the DataCollector in lib/Analysis/CloneDetection.cpp for a usage example.
23 ///
24 //===----------------------------------------------------------------------===//
25
26 #ifndef LLVM_CLANG_AST_DATACOLLECTION_H
27 #define LLVM_CLANG_AST_DATACOLLECTION_H
28
29 #include "clang/AST/ASTContext.h"
30
31 namespace clang {
32 namespace data_collection {
33
34 /// Returns a string that represents all macro expansions that expanded into the
35 /// given SourceLocation.
36 ///
37 /// If 'getMacroStack(A) == getMacroStack(B)' is true, then the SourceLocations
38 /// A and B are expanded from the same macros in the same order.
39 std::string getMacroStack(SourceLocation Loc, ASTContext &Context);
40
41 /// Utility functions for implementing addData() for a consumer that has a
42 /// method update(StringRef)
43 template <class T>
44 void addDataToConsumer(T &DataConsumer, llvm::StringRef Str) {
45 DataConsumer.update(Str);
46 }
47
48 template <class T> void addDataToConsumer(T &DataConsumer, const QualType &QT) {
49 addDataToConsumer(DataConsumer, QT.getAsString());
50 }
51
52 template <class T, class Type>
53 typename std::enable_if<
54 std::is_integral<Type>::value || std::is_enum<Type>::value ||
55 std::is_convertible<Type, size_t>::value // for llvm::hash_code
56 >::type
57 addDataToConsumer(T &DataConsumer, Type Data) {
58 DataConsumer.update(StringRef(reinterpret_cast<char *>(&Data), sizeof(Data)));
59 }
60
61 } // end namespace data_collection
62 } // end namespace clang
63
64 #endif // LLVM_CLANG_AST_DATACOLLECTION_H
ASTContext.h
Defines the clang::ASTContext interface.
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:654
clang::Type
The base class of the type hierarchy.
Definition: Type.h:1450
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:168
clang::data_collection::getMacroStack
std::string getMacroStack(SourceLocation Loc, ASTContext &Context)
Returns a string that represents all macro expansions that expanded into the given SourceLocation...
Definition: DataCollection.cpp:33
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:86
clang::data_collection::addDataToConsumer
void addDataToConsumer(T &DataConsumer, llvm::StringRef Str)
Utility functions for implementing addData() for a consumer that has a method update(StringRef) ...
Definition: DataCollection.h:44
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
type
clang::QualType::getAsString
static std::string getAsString(SplitQualType split, const PrintingPolicy &Policy)
Definition: Type.h:990