DataCollection.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- DatatCollection.h --------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. /// \file
  14. /// This file declares helper methods for collecting data from AST nodes.
  15. ///
  16. /// To collect data from Stmt nodes, subclass ConstStmtVisitor and include
  17. /// StmtDataCollectors.inc after defining the macros that you need. This
  18. /// provides data collection implementations for most Stmt kinds. Note
  19. /// that the code requires some conditions to be met:
  20. ///
  21. /// - There must be a method addData(const T &Data) that accepts strings,
  22. /// integral types as well as QualType. All data is forwarded using
  23. /// to this method.
  24. /// - The ASTContext of the Stmt must be accessible by the name Context.
  25. ///
  26. /// It is also possible to override individual visit methods. Have a look at
  27. /// the DataCollector in lib/Analysis/CloneDetection.cpp for a usage example.
  28. ///
  29. //===----------------------------------------------------------------------===//
  30. #ifndef LLVM_CLANG_AST_DATACOLLECTION_H
  31. #define LLVM_CLANG_AST_DATACOLLECTION_H
  32. #include "clang/AST/ASTContext.h"
  33. namespace clang {
  34. namespace data_collection {
  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. /// 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. template <class T> void addDataToConsumer(T &DataConsumer, const QualType &QT) {
  48. addDataToConsumer(DataConsumer, QT.getAsString());
  49. }
  50. template <class T, class Type>
  51. std::enable_if_t<std::is_integral<Type>::value || std::is_enum<Type>::value ||
  52. std::is_convertible<Type, size_t>::value // for llvm::hash_code
  53. >
  54. addDataToConsumer(T &DataConsumer, Type Data) {
  55. DataConsumer.update(StringRef(reinterpret_cast<char *>(&Data), sizeof(Data)));
  56. }
  57. } // end namespace data_collection
  58. } // end namespace clang
  59. #endif // LLVM_CLANG_AST_DATACOLLECTION_H
  60. #ifdef __GNUC__
  61. #pragma GCC diagnostic pop
  62. #endif