1 //===-- ODRHash.h - Hashing to diagnose ODR failures ------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_AST_ODRHASH_H
16 #define LLVM_CLANG_AST_ODRHASH_H
18 #include "clang/AST/DeclarationName.h"
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"
30 class NestedNameSpecifier;
32 class TemplateParameterList;
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
42 // Use DenseMaps to convert from DeclarationName and Type pointers
44 llvm::DenseMap<DeclarationName, unsigned> DeclNameMap;
46 // Save space by processing bools at the end.
47 llvm::SmallVector<bool, 128> Bools;
49 llvm::FoldingSetNodeID ID;
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);
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);
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);
67 // Process SubDecls of the main Decl. This method calls the DeclVisitor
68 // while AddDecl does not.
69 void AddSubDecl(const Decl *D);
71 // Reset the object for reuse.
74 // Add booleans to ID and uses it to calculate the hash.
75 unsigned CalculateHash();
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);
83 void AddNestedNameSpecifier(const NestedNameSpecifier *NNS);
84 void AddTemplateName(TemplateName Name);
85 void AddDeclarationName(DeclarationName Name, bool TreatAsDecl = false);
86 void AddTemplateArgument(TemplateArgument TA);
87 void AddTemplateParameterList(const TemplateParameterList *TPL);
89 // Save booleans until the end to lower the size of data to process.
90 void AddBoolean(bool value);
92 static bool isWhitelistedDecl(const Decl* D, const DeclContext *Parent);
95 void AddDeclarationNameImpl(DeclarationName Name);
98 } // end namespace clang