| 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 | 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 |
| 64 | |