]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/AST/ODRHash.h
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / AST / ODRHash.h
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 #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"
23
24 namespace clang {
25
26 class Decl;
27 class IdentifierInfo;
28 class NestedNameSpecifier;
29 class Stmt;
30 class TemplateParameterList;
31
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
38 // object.
39 class ODRHash {
40   // Use DenseMaps to convert from DeclarationName and Type pointers
41   // to an index value.
42   llvm::DenseMap<DeclarationName, unsigned> DeclNameMap;
43
44   // Save space by processing bools at the end.
45   llvm::SmallVector<bool, 128> Bools;
46
47   llvm::FoldingSetNodeID ID;
48
49 public:
50   ODRHash() {}
51
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);
55
56   // Use this for ODR checking functions between modules.  This method compares
57   // more information than the AddDecl class.  SkipBody will process the
58   // hash as if the function has no body.
59   void AddFunctionDecl(const FunctionDecl *Function, bool SkipBody = false);
60
61   // Use this for ODR checking enums between modules.  This method compares
62   // more information than the AddDecl class.
63   void AddEnumDecl(const EnumDecl *Enum);
64
65   // Process SubDecls of the main Decl.  This method calls the DeclVisitor
66   // while AddDecl does not.
67   void AddSubDecl(const Decl *D);
68
69   // Reset the object for reuse.
70   void clear();
71
72   // Add booleans to ID and uses it to calculate the hash.
73   unsigned CalculateHash();
74
75   // Add AST nodes that need to be processed.
76   void AddDecl(const Decl *D);
77   void AddType(const Type *T);
78   void AddQualType(QualType T);
79   void AddStmt(const Stmt *S);
80   void AddIdentifierInfo(const IdentifierInfo *II);
81   void AddNestedNameSpecifier(const NestedNameSpecifier *NNS);
82   void AddTemplateName(TemplateName Name);
83   void AddDeclarationName(DeclarationName Name);
84   void AddTemplateArgument(TemplateArgument TA);
85   void AddTemplateParameterList(const TemplateParameterList *TPL);
86
87   // Save booleans until the end to lower the size of data to process.
88   void AddBoolean(bool value);
89
90   static bool isWhitelistedDecl(const Decl* D, const DeclContext *Parent);
91 };
92
93 }  // end namespace clang