Home | History | Annotate | Download | only in AST
      1 //===--- DatatCollection.h --------------------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 /// \file
     10 /// \brief This file declares helper methods for collecting data from AST nodes.
     11 ///
     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 that code requires some conditions to be met:
     16 ///
     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
     19 ///     to this method.
     20 ///   - The ASTContext of the Stmt must be accessible by the name Context.
     21 ///
     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.
     24 ///
     25 //===----------------------------------------------------------------------===//
     26 
     27 #ifndef LLVM_CLANG_AST_DATACOLLECTION_H
     28 #define LLVM_CLANG_AST_DATACOLLECTION_H
     29 
     30 #include "clang/AST/ASTContext.h"
     31 
     32 namespace clang {
     33 namespace data_collection {
     34 
     35 /// Returns a string that represents all macro expansions that expanded into the
     36 /// given SourceLocation.
     37 ///
     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);
     41 
     42 /// Utility functions for implementing addData() for a consumer that has a
     43 /// method update(StringRef)
     44 template <class T>
     45 void addDataToConsumer(T &DataConsumer, llvm::StringRef Str) {
     46   DataConsumer.update(Str);
     47 }
     48 
     49 template <class T> void addDataToConsumer(T &DataConsumer, const QualType &QT) {
     50   addDataToConsumer(DataConsumer, QT.getAsString());
     51 }
     52 
     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
     57     >::type
     58 addDataToConsumer(T &DataConsumer, Type Data) {
     59   DataConsumer.update(StringRef(reinterpret_cast<char *>(&Data), sizeof(Data)));
     60 }
     61 
     62 } // end namespace data_collection
     63 } // end namespace clang
     64 
     65 #endif // LLVM_CLANG_AST_DATACOLLECTION_H
     66