ODRHash.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- ODRHash.h - Hashing to diagnose ODR failures ------------*- 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. ///
  14. /// \file
  15. /// This file contains the declaration of the ODRHash class, which calculates
  16. /// a hash based on AST nodes, which is stable across different runs.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_CLANG_AST_ODRHASH_H
  20. #define LLVM_CLANG_AST_ODRHASH_H
  21. #include "clang/AST/DeclarationName.h"
  22. #include "clang/AST/Type.h"
  23. #include "clang/AST/TemplateBase.h"
  24. #include "llvm/ADT/DenseMap.h"
  25. #include "llvm/ADT/FoldingSet.h"
  26. #include "llvm/ADT/PointerUnion.h"
  27. #include "llvm/ADT/SmallVector.h"
  28. namespace clang {
  29. class Decl;
  30. class IdentifierInfo;
  31. class NestedNameSpecifier;
  32. class Stmt;
  33. class TemplateParameterList;
  34. // ODRHash is used to calculate a hash based on AST node contents that
  35. // does not rely on pointer addresses. This allows the hash to not vary
  36. // between runs and is usable to detect ODR problems in modules. To use,
  37. // construct an ODRHash object, then call Add* methods over the nodes that
  38. // need to be hashed. Then call CalculateHash to get the hash value.
  39. // Typically, only one Add* call is needed. clear can be called to reuse the
  40. // object.
  41. class ODRHash {
  42. // Use DenseMaps to convert from DeclarationName and Type pointers
  43. // to an index value.
  44. llvm::DenseMap<DeclarationName, unsigned> DeclNameMap;
  45. // Save space by processing bools at the end.
  46. llvm::SmallVector<bool, 128> Bools;
  47. llvm::FoldingSetNodeID ID;
  48. public:
  49. ODRHash() {}
  50. // Use this for ODR checking classes between modules. This method compares
  51. // more information than the AddDecl class.
  52. void AddCXXRecordDecl(const CXXRecordDecl *Record);
  53. // Use this for ODR checking functions between modules. This method compares
  54. // more information than the AddDecl class. SkipBody will process the
  55. // hash as if the function has no body.
  56. void AddFunctionDecl(const FunctionDecl *Function, bool SkipBody = false);
  57. // Use this for ODR checking enums between modules. This method compares
  58. // more information than the AddDecl class.
  59. void AddEnumDecl(const EnumDecl *Enum);
  60. // Process SubDecls of the main Decl. This method calls the DeclVisitor
  61. // while AddDecl does not.
  62. void AddSubDecl(const Decl *D);
  63. // Reset the object for reuse.
  64. void clear();
  65. // Add booleans to ID and uses it to calculate the hash.
  66. unsigned CalculateHash();
  67. // Add AST nodes that need to be processed.
  68. void AddDecl(const Decl *D);
  69. void AddType(const Type *T);
  70. void AddQualType(QualType T);
  71. void AddStmt(const Stmt *S);
  72. void AddIdentifierInfo(const IdentifierInfo *II);
  73. void AddNestedNameSpecifier(const NestedNameSpecifier *NNS);
  74. void AddTemplateName(TemplateName Name);
  75. void AddDeclarationName(DeclarationName Name, bool TreatAsDecl = false);
  76. void AddTemplateArgument(TemplateArgument TA);
  77. void AddTemplateParameterList(const TemplateParameterList *TPL);
  78. // Save booleans until the end to lower the size of data to process.
  79. void AddBoolean(bool value);
  80. static bool isDeclToBeProcessed(const Decl* D, const DeclContext *Parent);
  81. private:
  82. void AddDeclarationNameImpl(DeclarationName Name);
  83. };
  84. } // end namespace clang
  85. #endif
  86. #ifdef __GNUC__
  87. #pragma GCC diagnostic pop
  88. #endif