Clang Project

clang_source_code/include/clang/AST/DataCollection.h
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
31namespace clang {
32namespace 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.
39std::string getMacroStack(SourceLocation LocASTContext &Context);
40
41/// Utility functions for implementing addData() for a consumer that has a
42/// method update(StringRef)
43template <class T>
44void addDataToConsumer(T &DataConsumerllvm::StringRef Str) {
45  DataConsumer.update(Str);
46}
47
48template <class T> void addDataToConsumer(T &DataConsumerconst QualType &QT) {
49  addDataToConsumer(DataConsumerQT.getAsString());
50}
51
52template <class T, class Type>
53typename 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
57addDataToConsumer(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
65