17330f729Sjoerg //===--- DatatCollection.h --------------------------------------*- C++ -*-===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg /// \file
97330f729Sjoerg /// This file declares helper methods for collecting data from AST nodes.
107330f729Sjoerg ///
117330f729Sjoerg /// To collect data from Stmt nodes, subclass ConstStmtVisitor and include
127330f729Sjoerg /// StmtDataCollectors.inc after defining the macros that you need. This
137330f729Sjoerg /// provides data collection implementations for most Stmt kinds. Note
147330f729Sjoerg /// that the code requires some conditions to be met:
157330f729Sjoerg ///
167330f729Sjoerg /// - There must be a method addData(const T &Data) that accepts strings,
177330f729Sjoerg /// integral types as well as QualType. All data is forwarded using
187330f729Sjoerg /// to this method.
197330f729Sjoerg /// - The ASTContext of the Stmt must be accessible by the name Context.
207330f729Sjoerg ///
217330f729Sjoerg /// It is also possible to override individual visit methods. Have a look at
227330f729Sjoerg /// the DataCollector in lib/Analysis/CloneDetection.cpp for a usage example.
237330f729Sjoerg ///
247330f729Sjoerg //===----------------------------------------------------------------------===//
257330f729Sjoerg
267330f729Sjoerg #ifndef LLVM_CLANG_AST_DATACOLLECTION_H
277330f729Sjoerg #define LLVM_CLANG_AST_DATACOLLECTION_H
287330f729Sjoerg
297330f729Sjoerg #include "clang/AST/ASTContext.h"
307330f729Sjoerg
317330f729Sjoerg namespace clang {
327330f729Sjoerg namespace data_collection {
337330f729Sjoerg
347330f729Sjoerg /// Returns a string that represents all macro expansions that expanded into the
357330f729Sjoerg /// given SourceLocation.
367330f729Sjoerg ///
377330f729Sjoerg /// If 'getMacroStack(A) == getMacroStack(B)' is true, then the SourceLocations
387330f729Sjoerg /// A and B are expanded from the same macros in the same order.
397330f729Sjoerg std::string getMacroStack(SourceLocation Loc, ASTContext &Context);
407330f729Sjoerg
417330f729Sjoerg /// Utility functions for implementing addData() for a consumer that has a
427330f729Sjoerg /// method update(StringRef)
437330f729Sjoerg template <class T>
addDataToConsumer(T & DataConsumer,llvm::StringRef Str)447330f729Sjoerg void addDataToConsumer(T &DataConsumer, llvm::StringRef Str) {
457330f729Sjoerg DataConsumer.update(Str);
467330f729Sjoerg }
477330f729Sjoerg
addDataToConsumer(T & DataConsumer,const QualType & QT)487330f729Sjoerg template <class T> void addDataToConsumer(T &DataConsumer, const QualType &QT) {
497330f729Sjoerg addDataToConsumer(DataConsumer, QT.getAsString());
507330f729Sjoerg }
517330f729Sjoerg
527330f729Sjoerg template <class T, class Type>
53*e038c9c4Sjoerg std::enable_if_t<std::is_integral<Type>::value || std::is_enum<Type>::value ||
547330f729Sjoerg std::is_convertible<Type, size_t>::value // for llvm::hash_code
55*e038c9c4Sjoerg >
addDataToConsumer(T & DataConsumer,Type Data)567330f729Sjoerg addDataToConsumer(T &DataConsumer, Type Data) {
577330f729Sjoerg DataConsumer.update(StringRef(reinterpret_cast<char *>(&Data), sizeof(Data)));
587330f729Sjoerg }
597330f729Sjoerg
607330f729Sjoerg } // end namespace data_collection
617330f729Sjoerg } // end namespace clang
627330f729Sjoerg
637330f729Sjoerg #endif // LLVM_CLANG_AST_DATACOLLECTION_H
64