From 9b7e574701554fd496dc0fafe567b60767783b4b Mon Sep 17 00:00:00 2001 From: Chuanqi Xu Date: Fri, 3 Feb 2023 14:29:14 +0800 Subject: [PATCH] [NFC] [Serialization] Add static assert for Num*Declbits This re-commits part of c79635cce845. It is reverted since it contains platform-inconsistent constant. Now the patch only contains constant defined in DeclBase.h so it should be platform-independent. And this should be still helpful. Reviewed By: erichkeane Differential Revision: https://reviews.llvm.org/D141992 --- clang/include/clang/AST/DeclBase.h | 2 ++ clang/lib/Serialization/ASTWriterDecl.cpp | 40 +++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/clang/include/clang/AST/DeclBase.h b/clang/include/clang/AST/DeclBase.h index 6134fdd..ef5d4d7 100644 --- a/clang/include/clang/AST/DeclBase.h +++ b/clang/include/clang/AST/DeclBase.h @@ -1389,6 +1389,8 @@ public: class DeclContext { /// For makeDeclVisibleInContextImpl friend class ASTDeclReader; + /// For checking the new bits in the Serialization part. + friend class ASTDeclWriter; /// For reconcileExternalVisibleStorage, CreateStoredDeclsMap, /// hasNeedToReconcileExternalVisibleStorage friend class ExternalASTSource; diff --git a/clang/lib/Serialization/ASTWriterDecl.cpp b/clang/lib/Serialization/ASTWriterDecl.cpp index a58e0d7..9e1e58a 100644 --- a/clang/lib/Serialization/ASTWriterDecl.cpp +++ b/clang/lib/Serialization/ASTWriterDecl.cpp @@ -411,6 +411,10 @@ void ASTDeclWriter::VisitTypeAliasDecl(TypeAliasDecl *D) { } void ASTDeclWriter::VisitTagDecl(TagDecl *D) { + static_assert(DeclContext::NumTagDeclBits == 10, + "You need to update the serializer after you change the " + "TagDeclBits"); + VisitRedeclarable(D); VisitTypeDecl(D); Record.push_back(D->getIdentifierNamespace()); @@ -435,6 +439,10 @@ void ASTDeclWriter::VisitTagDecl(TagDecl *D) { } void ASTDeclWriter::VisitEnumDecl(EnumDecl *D) { + static_assert(DeclContext::NumEnumDeclBits == 20, + "You need to update the serializer after you change the " + "EnumDeclBits"); + VisitTagDecl(D); Record.AddTypeSourceInfo(D->getIntegerTypeSourceInfo()); if (!D->getIntegerTypeSourceInfo()) @@ -478,6 +486,10 @@ void ASTDeclWriter::VisitEnumDecl(EnumDecl *D) { } void ASTDeclWriter::VisitRecordDecl(RecordDecl *D) { + static_assert(DeclContext::NumRecordDeclBits == 41, + "You need to update the serializer after you change the " + "RecordDeclBits"); + VisitTagDecl(D); Record.push_back(D->hasFlexibleArrayMember()); Record.push_back(D->isAnonymousStructOrUnion()); @@ -546,6 +558,10 @@ void ASTDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) { } void ASTDeclWriter::VisitFunctionDecl(FunctionDecl *D) { + static_assert(DeclContext::NumFunctionDeclBits == 29, + "You need to update the serializer after you change the " + "FunctionDeclBits"); + VisitRedeclarable(D); Record.push_back(D->getTemplatedKind()); @@ -697,6 +713,10 @@ void ASTDeclWriter::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) { } void ASTDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) { + static_assert(DeclContext::NumObjCMethodDeclBits == 24, + "You need to update the serializer after you change the " + "ObjCMethodDeclBits"); + VisitNamedDecl(D); // FIXME: convert to LazyStmtPtr? // Unlike C/C++, method bodies will never be in header files. @@ -755,6 +775,10 @@ void ASTDeclWriter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) { } void ASTDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) { + static_assert(DeclContext::NumObjCContainerDeclBits == 51, + "You need to update the serializer after you change the " + "ObjCContainerDeclBits"); + VisitNamedDecl(D); Record.AddSourceLocation(D->getAtStartLoc()); Record.AddSourceRange(D->getAtEndRange()); @@ -1241,6 +1265,10 @@ void ASTDeclWriter::VisitCapturedDecl(CapturedDecl *CD) { } void ASTDeclWriter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { + static_assert(DeclContext::NumLinkageSpecDeclBits == 4, + "You need to update the serializer after you change the" + "LinkageSpecDeclBits"); + VisitDecl(D); Record.push_back(D->getLanguage()); Record.AddSourceLocation(D->getExternLoc()); @@ -1436,6 +1464,10 @@ void ASTDeclWriter::VisitCXXMethodDecl(CXXMethodDecl *D) { } void ASTDeclWriter::VisitCXXConstructorDecl(CXXConstructorDecl *D) { + static_assert(DeclContext::NumCXXConstructorDeclBits == 22, + "You need to update the serializer after you change the " + "CXXConstructorDeclBits"); + Record.push_back(D->getTrailingAllocKind()); addExplicitSpecifier(D->getExplicitSpecifier(), Record); if (auto Inherited = D->getInheritedConstructor()) { @@ -1812,6 +1844,10 @@ void ASTDeclWriter::VisitStaticAssertDecl(StaticAssertDecl *D) { /// Emit the DeclContext part of a declaration context decl. void ASTDeclWriter::VisitDeclContext(DeclContext *DC) { + static_assert(DeclContext::NumDeclContextBits == 13, + "You need to update the serializer after you change the " + "DeclContextBits"); + Record.AddOffset(Writer.WriteDeclContextLexicalBlock(Context, DC)); Record.AddOffset(Writer.WriteDeclContextVisibleBlock(Context, DC)); } @@ -1922,6 +1958,10 @@ void ASTDeclWriter::VisitOMPRequiresDecl(OMPRequiresDecl *D) { } void ASTDeclWriter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) { + static_assert(DeclContext::NumOMPDeclareReductionDeclBits == 2, + "You need to update the serializer after you change the " + "NumOMPDeclareReductionDeclBits"); + VisitValueDecl(D); Record.AddSourceLocation(D->getBeginLoc()); Record.AddStmt(D->getCombinerIn()); -- 2.7.4