clang  14.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.
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 std::enable_if_t<std::is_integral<Type>::value || std::is_enum<Type>::value ||
54  std::is_convertible<Type, size_t>::value // for llvm::hash_code
55  >
56 addDataToConsumer(T &DataConsumer, Type Data) {
57  DataConsumer.update(StringRef(reinterpret_cast<char *>(&Data), sizeof(Data)));
58 }
59 
60 } // end namespace data_collection
61 } // end namespace clang
62 
63 #endif // LLVM_CLANG_AST_DATACOLLECTION_H
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:673
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::QualType::getAsString
static std::string getAsString(SplitQualType split, const PrintingPolicy &Policy)
Definition: Type.h:1015
clang::Type
The base class of the type hierarchy.
Definition: Type.h:1490
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:212
ASTContext.h
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
Definition: CalledOnceCheck.h:17