1 //===--- DatatCollection.h --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This file declares helper methods for collecting data from AST nodes.
12 /// To collect data from Stmt nodes, subclass ConstStmtVisitor and include
13 /// StmtDataCollectors.inc after defining the macros that you need. This
14 /// provides data collection implementations for most Stmt kinds. Note
15 /// that the code requires some conditions to be met:
17 /// - There must be a method addData(const T &Data) that accepts strings,
18 /// integral types as well as QualType. All data is forwarded using
20 /// - The ASTContext of the Stmt must be accessible by the name Context.
22 /// It is also possible to override individual visit methods. Have a look at
23 /// the DataCollector in lib/Analysis/CloneDetection.cpp for a usage example.
25 //===----------------------------------------------------------------------===//
27 #ifndef LLVM_CLANG_AST_DATACOLLECTION_H
28 #define LLVM_CLANG_AST_DATACOLLECTION_H
30 #include "clang/AST/ASTContext.h"
33 namespace data_collection {
35 /// Returns a string that represents all macro expansions that expanded into the
36 /// given SourceLocation.
38 /// If 'getMacroStack(A) == getMacroStack(B)' is true, then the SourceLocations
39 /// A and B are expanded from the same macros in the same order.
40 std::string getMacroStack(SourceLocation Loc, ASTContext &Context);
42 /// Utility functions for implementing addData() for a consumer that has a
43 /// method update(StringRef)
45 void addDataToConsumer(T &DataConsumer, llvm::StringRef Str) {
46 DataConsumer.update(Str);
49 template <class T> void addDataToConsumer(T &DataConsumer, const QualType &QT) {
50 addDataToConsumer(DataConsumer, QT.getAsString());
53 template <class T, class Type>
54 typename std::enable_if<
55 std::is_integral<Type>::value || std::is_enum<Type>::value ||
56 std::is_convertible<Type, size_t>::value // for llvm::hash_code
58 addDataToConsumer(T &DataConsumer, Type Data) {
59 DataConsumer.update(StringRef(reinterpret_cast<char *>(&Data), sizeof(Data)));
62 } // end namespace data_collection
63 } // end namespace clang
65 #endif // LLVM_CLANG_AST_DATACOLLECTION_H