clang  10.0.0svn
ODRHash.h
Go to the documentation of this file.
1 //===-- ODRHash.h - Hashing to diagnose ODR failures ------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file contains the declaration of the ODRHash class, which calculates
11 /// a hash based on AST nodes, which is stable across different runs.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_AST_ODRHASH_H
16 #define LLVM_CLANG_AST_ODRHASH_H
17 
19 #include "clang/AST/Type.h"
20 #include "clang/AST/TemplateBase.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/FoldingSet.h"
23 #include "llvm/ADT/PointerUnion.h"
24 #include "llvm/ADT/SmallVector.h"
25 
26 namespace clang {
27 
28 class Decl;
29 class IdentifierInfo;
30 class NestedNameSpecifier;
31 class Stmt;
32 class TemplateParameterList;
33 
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 
46  // Save space by processing bools at the end.
48 
49  llvm::FoldingSetNodeID ID;
50 
51 public:
52  ODRHash() {}
53 
54  // Use this for ODR checking classes between modules. This method compares
55  // more information than the AddDecl class.
56  void AddCXXRecordDecl(const CXXRecordDecl *Record);
57 
58  // Use this for ODR checking functions between modules. This method compares
59  // more information than the AddDecl class. SkipBody will process the
60  // hash as if the function has no body.
61  void AddFunctionDecl(const FunctionDecl *Function, bool SkipBody = false);
62 
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 
67  // Process SubDecls of the main Decl. This method calls the DeclVisitor
68  // while AddDecl does not.
69  void AddSubDecl(const Decl *D);
70 
71  // Reset the object for reuse.
72  void clear();
73 
74  // Add booleans to ID and uses it to calculate the hash.
75  unsigned CalculateHash();
76 
77  // Add AST nodes that need to be processed.
78  void AddDecl(const Decl *D);
79  void AddType(const Type *T);
80  void AddQualType(QualType T);
81  void AddStmt(const Stmt *S);
82  void AddIdentifierInfo(const IdentifierInfo *II);
84  void AddTemplateName(TemplateName Name);
85  void AddDeclarationName(DeclarationName Name, bool TreatAsDecl = false);
88 
89  // Save booleans until the end to lower the size of data to process.
90  void AddBoolean(bool value);
91 
92  static bool isWhitelistedDecl(const Decl* D, const DeclContext *Parent);
93 
94 private:
95  void AddDeclarationNameImpl(DeclarationName Name);
96 };
97 
98 } // end namespace clang
99 
100 #endif
Represents a function declaration or definition.
Definition: Decl.h:1784
A (possibly-)qualified type.
Definition: Type.h:643
void AddBoolean(bool value)
Definition: ODRHash.cpp:1121
void AddDeclarationName(DeclarationName Name, bool TreatAsDecl=false)
Definition: ODRHash.cpp:34
Stmt - This represents one statement.
Definition: Stmt.h:66
void AddQualType(QualType T)
Definition: ODRHash.cpp:1112
C Language Family Type Representation.
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:88
The base class of the type hierarchy.
Definition: Type.h:1436
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
Stores a list of template parameters for a TemplateDecl and its derived classes.
Definition: DeclTemplate.h:67
void AddTemplateArgument(TemplateArgument TA)
Definition: ODRHash.cpp:157
void clear()
Definition: ODRHash.cpp:198
One of these records is kept for each identifier that is lexed.
void AddTemplateParameterList(const TemplateParameterList *TPL)
Definition: ODRHash.cpp:189
void AddDecl(const Decl *D)
Definition: ODRHash.cpp:625
void AddFunctionDecl(const FunctionDecl *Function, bool SkipBody=false)
Definition: ODRHash.cpp:514
void AddTemplateName(TemplateName Name)
Definition: ODRHash.cpp:138
NodeId Parent
Definition: ASTDiff.cpp:191
Represents a C++ template name within the type system.
Definition: TemplateName.h:187
void AddType(const Type *T)
Definition: ODRHash.cpp:1107
void AddIdentifierInfo(const IdentifierInfo *II)
Definition: ODRHash.cpp:29
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
void AddNestedNameSpecifier(const NestedNameSpecifier *NNS)
Definition: ODRHash.cpp:109
void AddSubDecl(const Decl *D)
Definition: ODRHash.cpp:461
void AddEnumDecl(const EnumDecl *Enum)
Definition: ODRHash.cpp:597
Represents a template argument.
Definition: TemplateBase.h:50
Dataflow Directional Tag Classes.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1271
void AddCXXRecordDecl(const CXXRecordDecl *Record)
Definition: ODRHash.cpp:467
The name of a declaration.
Represents an enum.
Definition: Decl.h:3395
unsigned CalculateHash()
Definition: ODRHash.cpp:204
Represents a C++ struct/union/class.
Definition: DeclCXX.h:255
void AddStmt(const Stmt *S)
Definition: ODRHash.cpp:24
static bool isWhitelistedDecl(const Decl *D, const DeclContext *Parent)
Definition: ODRHash.cpp:438