1 //===-- ODRHash.h - Hashing to diagnose ODR failures ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file contains the declaration of the ODRHash class, which calculates
12 /// a hash based on AST nodes, which is stable across different runs.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CLANG_AST_ODRHASH_H
17 #define LLVM_CLANG_AST_ODRHASH_H
19 #include "clang/AST/DeclarationName.h"
20 #include "clang/AST/Type.h"
21 #include "clang/AST/TemplateBase.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/FoldingSet.h"
24 #include "llvm/ADT/PointerUnion.h"
25 #include "llvm/ADT/SmallVector.h"
31 class NestedNameSpecifier;
33 class TemplateParameterList;
35 // ODRHash is used to calculate a hash based on AST node contents that
36 // does not rely on pointer addresses. This allows the hash to not vary
37 // between runs and is usable to detect ODR problems in modules. To use,
38 // construct an ODRHash object, then call Add* methods over the nodes that
39 // need to be hashed. Then call CalculateHash to get the hash value.
40 // Typically, only one Add* call is needed. clear can be called to reuse the
43 // Use DenseMaps to convert from DeclarationName and Type pointers
45 llvm::DenseMap<DeclarationName, unsigned> DeclNameMap;
47 // Save space by processing bools at the end.
48 llvm::SmallVector<bool, 128> Bools;
50 llvm::FoldingSetNodeID ID;
55 // Use this for ODR checking classes between modules. This method compares
56 // more information than the AddDecl class.
57 void AddCXXRecordDecl(const CXXRecordDecl *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);
64 // Use this for ODR checking enums between modules. This method compares
65 // more information than the AddDecl class.
66 void AddEnumDecl(const EnumDecl *Enum);
68 // Process SubDecls of the main Decl. This method calls the DeclVisitor
69 // while AddDecl does not.
70 void AddSubDecl(const Decl *D);
72 // Reset the object for reuse.
75 // Add booleans to ID and uses it to calculate the hash.
76 unsigned CalculateHash();
78 // Add AST nodes that need to be processed.
79 void AddDecl(const Decl *D);
80 void AddType(const Type *T);
81 void AddQualType(QualType T);
82 void AddStmt(const Stmt *S);
83 void AddIdentifierInfo(const IdentifierInfo *II);
84 void AddNestedNameSpecifier(const NestedNameSpecifier *NNS);
85 void AddTemplateName(TemplateName Name);
86 void AddDeclarationName(DeclarationName Name, bool TreatAsDecl = false);
87 void AddTemplateArgument(TemplateArgument TA);
88 void AddTemplateParameterList(const TemplateParameterList *TPL);
90 // Save booleans until the end to lower the size of data to process.
91 void AddBoolean(bool value);
93 static bool isWhitelistedDecl(const Decl* D, const DeclContext *Parent);
96 void AddDeclarationNameImpl(DeclarationName Name);
99 } // end namespace clang