Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:38

0001 //===-- ODRHash.h - Hashing to diagnose ODR failures ------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 ///
0009 /// \file
0010 /// This file contains the declaration of the ODRHash class, which calculates
0011 /// a hash based on AST nodes, which is stable across different runs.
0012 ///
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_CLANG_AST_ODRHASH_H
0016 #define LLVM_CLANG_AST_ODRHASH_H
0017 
0018 #include "clang/AST/DeclarationName.h"
0019 #include "clang/AST/Type.h"
0020 #include "clang/AST/TemplateBase.h"
0021 #include "llvm/ADT/DenseMap.h"
0022 #include "llvm/ADT/FoldingSet.h"
0023 #include "llvm/ADT/PointerUnion.h"
0024 #include "llvm/ADT/SmallVector.h"
0025 
0026 namespace clang {
0027 
0028 class APValue;
0029 class Decl;
0030 class IdentifierInfo;
0031 class NestedNameSpecifier;
0032 class Stmt;
0033 class TemplateParameterList;
0034 
0035 // ODRHash is used to calculate a hash based on AST node contents that
0036 // does not rely on pointer addresses.  This allows the hash to not vary
0037 // between runs and is usable to detect ODR problems in modules.  To use,
0038 // construct an ODRHash object, then call Add* methods over the nodes that
0039 // need to be hashed.  Then call CalculateHash to get the hash value.
0040 // Typically, only one Add* call is needed.  clear can be called to reuse the
0041 // object.
0042 class ODRHash {
0043   // Use DenseMaps to convert from DeclarationName and Type pointers
0044   // to an index value.
0045   llvm::DenseMap<DeclarationName, unsigned> DeclNameMap;
0046 
0047   // Save space by processing bools at the end.
0048   llvm::SmallVector<bool, 128> Bools;
0049 
0050   llvm::FoldingSetNodeID ID;
0051 
0052 public:
0053   ODRHash() {}
0054 
0055   // Use this for ODR checking classes between modules.  This method compares
0056   // more information than the AddDecl class.
0057   void AddCXXRecordDecl(const CXXRecordDecl *Record);
0058 
0059   // Use this for ODR checking records in C/Objective-C between modules. This
0060   // method compares more information than the AddDecl class.
0061   void AddRecordDecl(const RecordDecl *Record);
0062 
0063   // Use this for ODR checking ObjC interfaces. This
0064   // method compares more information than the AddDecl class.
0065   void AddObjCInterfaceDecl(const ObjCInterfaceDecl *Record);
0066 
0067   // Use this for ODR checking functions between modules.  This method compares
0068   // more information than the AddDecl class.  SkipBody will process the
0069   // hash as if the function has no body.
0070   void AddFunctionDecl(const FunctionDecl *Function, bool SkipBody = false);
0071 
0072   // Use this for ODR checking enums between modules.  This method compares
0073   // more information than the AddDecl class.
0074   void AddEnumDecl(const EnumDecl *Enum);
0075 
0076   // Use this for ODR checking ObjC protocols. This
0077   // method compares more information than the AddDecl class.
0078   void AddObjCProtocolDecl(const ObjCProtocolDecl *P);
0079 
0080   // Process SubDecls of the main Decl.  This method calls the DeclVisitor
0081   // while AddDecl does not.
0082   void AddSubDecl(const Decl *D);
0083 
0084   // Reset the object for reuse.
0085   void clear();
0086 
0087   // Add booleans to ID and uses it to calculate the hash.
0088   unsigned CalculateHash();
0089 
0090   // Add AST nodes that need to be processed.
0091   void AddDecl(const Decl *D);
0092   void AddType(const Type *T);
0093   void AddQualType(QualType T);
0094   void AddStmt(const Stmt *S);
0095   void AddIdentifierInfo(const IdentifierInfo *II);
0096   void AddNestedNameSpecifier(const NestedNameSpecifier *NNS);
0097   void AddTemplateName(TemplateName Name);
0098   void AddDeclarationName(DeclarationName Name, bool TreatAsDecl = false);
0099   void AddTemplateArgument(TemplateArgument TA);
0100   void AddTemplateParameterList(const TemplateParameterList *TPL);
0101 
0102   // Save booleans until the end to lower the size of data to process.
0103   void AddBoolean(bool value);
0104 
0105   void AddStructuralValue(const APValue &);
0106 
0107   static bool isSubDeclToBeProcessed(const Decl *D, const DeclContext *Parent);
0108 
0109 private:
0110   void AddDeclarationNameImpl(DeclarationName Name);
0111 };
0112 
0113 }  // end namespace clang
0114 
0115 #endif