1 //===-- ODRHash.h - Hashing to diagnose ODR failures ------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
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.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CLANG_AST_ODRHASH_H
17 #define LLVM_CLANG_AST_ODRHASH_H
18 
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"
26 
27 namespace clang {
28 
29 class Decl;
30 class IdentifierInfo;
31 class NestedNameSpecifier;
32 class Stmt;
33 class TemplateParameterList;
34 
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
41 // object.
42 class ODRHash {
43   // Use DenseMaps to convert from DeclarationName and Type pointers
44   // to an index value.
45   llvm::DenseMap<DeclarationName, unsigned> DeclNameMap;
46 
47   // Save space by processing bools at the end.
48   llvm::SmallVector<bool, 128> Bools;
49 
50   llvm::FoldingSetNodeID ID;
51 
52 public:
ODRHash()53   ODRHash() {}
54 
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);
58 
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 
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);
67 
68   // Process SubDecls of the main Decl.  This method calls the DeclVisitor
69   // while AddDecl does not.
70   void AddSubDecl(const Decl *D);
71 
72   // Reset the object for reuse.
73   void clear();
74 
75   // Add booleans to ID and uses it to calculate the hash.
76   unsigned CalculateHash();
77 
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);
89 
90   // Save booleans until the end to lower the size of data to process.
91   void AddBoolean(bool value);
92 
93   static bool isWhitelistedDecl(const Decl* D, const DeclContext *Parent);
94 
95 private:
96   void AddDeclarationNameImpl(DeclarationName Name);
97 };
98 
99 }  // end namespace clang
100 
101 #endif
102