Author: Aaron Ballman Date: 2023-04-10T08:29:21-04:00 New Revision: 636dd1e8a1782e22f9bdee640428ed5c50a4a4f2
URL: https://github.com/llvm/llvm-project/commit/636dd1e8a1782e22f9bdee640428ed5c50a4a4f2 DIFF: https://github.com/llvm/llvm-project/commit/636dd1e8a1782e22f9bdee640428ed5c50a4a4f2.diff LOG: Make explicit the single-argument constructors of AttributeCommonInfo; NFC The single-argument constructors of this class were not marked explicit and that led to some incorrect uses that slipped under the radar (see changes in SemaDeclAttr.cpp). This makes the constructors explicit, changes the benignly incorrect uses, and updates the tablegen code to emit the correct support code to call the explicit constructors. While this does correct a misuse, that incorrect usage could not be observed except via a debugger and so no additional tests are added. Differential Revision: https://reviews.llvm.org/D147661 Added: Modified: clang/include/clang/Basic/Attr.td clang/include/clang/Basic/AttributeCommonInfo.h clang/lib/Sema/SemaDeclAttr.cpp clang/utils/TableGen/ClangAttrEmitter.cpp Removed: ################################################################################ diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index c8f364faecabd..79da53aba39ff 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -834,7 +834,7 @@ def Annotate : InheritableParamAttr { return AnnotateAttr::Create(Ctx, Annotation, nullptr, 0, CommonInfo); } static AnnotateAttr *CreateImplicit(ASTContext &Ctx, llvm::StringRef Annotation, \ - const AttributeCommonInfo &CommonInfo = {SourceRange{}}) { + const AttributeCommonInfo &CommonInfo = AttributeCommonInfo{SourceRange{}}) { return AnnotateAttr::CreateImplicit(Ctx, Annotation, nullptr, 0, CommonInfo); } }]; diff --git a/clang/include/clang/Basic/AttributeCommonInfo.h b/clang/include/clang/Basic/AttributeCommonInfo.h index 81a8d21341938..f89bdbdaa0d28 100644 --- a/clang/include/clang/Basic/AttributeCommonInfo.h +++ b/clang/include/clang/Basic/AttributeCommonInfo.h @@ -76,11 +76,11 @@ class AttributeCommonInfo { static constexpr unsigned SpellingNotCalculated = 0xf; public: - AttributeCommonInfo(SourceRange AttrRange) + explicit AttributeCommonInfo(SourceRange AttrRange) : AttrRange(AttrRange), ScopeLoc(), AttrKind(0), SyntaxUsed(0), SpellingIndex(SpellingNotCalculated) {} - AttributeCommonInfo(SourceLocation AttrLoc) + explicit AttributeCommonInfo(SourceLocation AttrLoc) : AttrRange(AttrLoc), ScopeLoc(), AttrKind(0), SyntaxUsed(0), SpellingIndex(SpellingNotCalculated) {} diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp index 19bc03d30a21e..5e98dca657469 100644 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -2775,7 +2775,7 @@ static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) { return V; }; AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr( - ND, AL.getRange(), NewII, true /*Implicit*/, + ND, AL, NewII, true /*Implicit*/, MinMacCatalystVersion(Introduced.Version), MinMacCatalystVersion(Deprecated.Version), MinMacCatalystVersion(Obsoleted.Version), IsUnavailable, Str, @@ -2817,7 +2817,7 @@ static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) { return V ? *V : VersionTuple(); }; AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr( - ND, AL.getRange(), NewII, true /*Implicit*/, + ND, AL, NewII, true /*Implicit*/, VersionOrEmptyVersion(NewIntroduced), VersionOrEmptyVersion(NewDeprecated), VersionOrEmptyVersion(NewObsoleted), /*IsUnavailable=*/false, Str, diff --git a/clang/utils/TableGen/ClangAttrEmitter.cpp b/clang/utils/TableGen/ClangAttrEmitter.cpp index d8a651606e9bd..84cbf60ad05d2 100644 --- a/clang/utils/TableGen/ClangAttrEmitter.cpp +++ b/clang/utils/TableGen/ClangAttrEmitter.cpp @@ -2505,8 +2505,15 @@ static void emitAttributes(RecordKeeper &Records, raw_ostream &OS, return &R == P.second; }); + enum class CreateKind { + WithAttributeCommonInfo, + WithSourceRange, + WithNoArgs, + }; + // Emit CreateImplicit factory methods. - auto emitCreate = [&](bool Implicit, bool DelayedArgsOnly, bool emitFake) { + auto emitCreate = [&](bool Implicit, bool DelayedArgsOnly, + bool emitFake, CreateKind Kind) { if (Header) OS << " static "; OS << R.getName() << "Attr *"; @@ -2530,9 +2537,10 @@ static void emitAttributes(RecordKeeper &Records, raw_ostream &OS, OS << ", "; DelayedArgs->writeCtorParameters(OS); } - OS << ", const AttributeCommonInfo &CommonInfo"; - if (Header && Implicit) - OS << " = {SourceRange{}}"; + if (Kind == CreateKind::WithAttributeCommonInfo) + OS << ", const AttributeCommonInfo &CommonInfo"; + else if (Kind == CreateKind::WithSourceRange) + OS << ", SourceRange R"; OS << ")"; if (Header) { OS << ";\n"; @@ -2541,7 +2549,13 @@ static void emitAttributes(RecordKeeper &Records, raw_ostream &OS, OS << " {\n"; OS << " auto *A = new (Ctx) " << R.getName(); - OS << "Attr(Ctx, CommonInfo"; + if (Kind == CreateKind::WithAttributeCommonInfo) + OS << "Attr(Ctx, CommonInfo"; + else if (Kind == CreateKind::WithSourceRange) + OS << "Attr(Ctx, AttributeCommonInfo{R}"; + else if (Kind == CreateKind::WithNoArgs) + OS << "Attr(Ctx, AttributeCommonInfo{SourceLocation{}}"; + if (!DelayedArgsOnly) { for (auto const &ai : Args) { if (ai->isFake() && !emitFake) @@ -2637,9 +2651,19 @@ static void emitAttributes(RecordKeeper &Records, raw_ostream &OS, OS << "}\n\n"; }; + auto emitBothImplicitAndNonCreates = [&](bool DelayedArgsOnly, + bool emitFake, CreateKind Kind) { + emitCreate(true, DelayedArgsOnly, emitFake, Kind); + emitCreate(false, DelayedArgsOnly, emitFake, Kind); + }; + auto emitCreates = [&](bool DelayedArgsOnly, bool emitFake) { - emitCreate(true, DelayedArgsOnly, emitFake); - emitCreate(false, DelayedArgsOnly, emitFake); + emitBothImplicitAndNonCreates(DelayedArgsOnly, emitFake, + CreateKind::WithNoArgs); + emitBothImplicitAndNonCreates(DelayedArgsOnly, emitFake, + CreateKind::WithAttributeCommonInfo); + emitBothImplicitAndNonCreates(DelayedArgsOnly, emitFake, + CreateKind::WithSourceRange); emitCreateNoCI(true, DelayedArgsOnly, emitFake); emitCreateNoCI(false, DelayedArgsOnly, emitFake); }; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits