ODRHash.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 records in C/Objective-C between modules. This
  54. // method compares more information than the AddDecl class.
  55. void AddRecordDecl(const RecordDecl *Record);
  56. // Use this for ODR checking ObjC interfaces. This
  57. // method compares more information than the AddDecl class.
  58. void AddObjCInterfaceDecl(const ObjCInterfaceDecl *Record);
  59. // Use this for ODR checking functions between modules. This method compares
  60. // more information than the AddDecl class. SkipBody will process the
  61. // hash as if the function has no body.
  62. void AddFunctionDecl(const FunctionDecl *Function, bool SkipBody = false);
  63. // Use this for ODR checking enums between modules. This method compares
  64. // more information than the AddDecl class.
  65. void AddEnumDecl(const EnumDecl *Enum);
  66. // Use this for ODR checking ObjC protocols. This
  67. // method compares more information than the AddDecl class.
  68. void AddObjCProtocolDecl(const ObjCProtocolDecl *P);
  69. // Process SubDecls of the main Decl. This method calls the DeclVisitor
  70. // while AddDecl does not.
  71. void AddSubDecl(const Decl *D);
  72. // Reset the object for reuse.
  73. void clear();
  74. // Add booleans to ID and uses it to calculate the hash.
  75. unsigned CalculateHash();
  76. // Add AST nodes that need to be processed.
  77. void AddDecl(const Decl *D);
  78. void AddType(const Type *T);
  79. void AddQualType(QualType T);
  80. void AddStmt(const Stmt *S);
  81. void AddIdentifierInfo(const IdentifierInfo *II);
  82. void AddNestedNameSpecifier(const NestedNameSpecifier *NNS);
  83. void AddTemplateName(TemplateName Name);
  84. void AddDeclarationName(DeclarationName Name, bool TreatAsDecl = false);
  85. void AddTemplateArgument(TemplateArgument TA);
  86. void AddTemplateParameterList(const TemplateParameterList *TPL);
  87. // Save booleans until the end to lower the size of data to process.
  88. void AddBoolean(bool value);
  89. static bool isSubDeclToBeProcessed(const Decl *D, const DeclContext *Parent);
  90. private:
  91. void AddDeclarationNameImpl(DeclarationName Name);
  92. };
  93. } // end namespace clang
  94. #endif
  95. #ifdef __GNUC__
  96. #pragma GCC diagnostic pop
  97. #endif