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 #include "clang/AST/DeclarationName.h"
17 #include "clang/AST/Type.h"
18 #include "clang/AST/TemplateBase.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/PointerUnion.h"
22 #include "llvm/ADT/SmallVector.h"
28 class NestedNameSpecifier;
30 class TemplateParameterList;
32 // ODRHash is used to calculate a hash based on AST node contents that
33 // does not rely on pointer addresses. This allows the hash to not vary
34 // between runs and is usable to detect ODR problems in modules. To use,
35 // construct an ODRHash object, then call Add* methods over the nodes that
36 // need to be hashed. Then call CalculateHash to get the hash value.
37 // Typically, only one Add* call is needed. clear can be called to reuse the
40 // Use DenseMaps to convert between Decl and Type pointers and an index value.
41 llvm::DenseMap<const Decl*, unsigned> DeclMap;
42 llvm::DenseMap<const Type*, unsigned> TypeMap;
44 // Save space by processing bools at the end.
45 llvm::SmallVector<bool, 128> Bools;
47 llvm::FoldingSetNodeID ID;
52 // Use this for ODR checking classes between modules. This method compares
53 // more information than the AddDecl class.
54 void AddCXXRecordDecl(const CXXRecordDecl *Record);
56 // Use this for ODR checking functions between modules. This method compares
57 // more information than the AddDecl class.
58 void AddFunctionDecl(const FunctionDecl *Function);
60 // Process SubDecls of the main Decl. This method calls the DeclVisitor
61 // while AddDecl does not.
62 void AddSubDecl(const Decl *D);
64 // Reset the object for reuse.
67 // Add booleans to ID and uses it to calculate the hash.
68 unsigned CalculateHash();
70 // Add AST nodes that need to be processed.
71 void AddDecl(const Decl *D);
72 void AddType(const Type *T);
73 void AddQualType(QualType T);
74 void AddStmt(const Stmt *S);
75 void AddIdentifierInfo(const IdentifierInfo *II);
76 void AddNestedNameSpecifier(const NestedNameSpecifier *NNS);
77 void AddTemplateName(TemplateName Name);
78 void AddDeclarationName(DeclarationName Name);
79 void AddTemplateArgument(TemplateArgument TA);
80 void AddTemplateParameterList(const TemplateParameterList *TPL);
82 // Save booleans until the end to lower the size of data to process.
83 void AddBoolean(bool value);
85 static bool isWhitelistedDecl(const Decl* D, const CXXRecordDecl *Record);
88 } // end namespace clang