Skip to content

Commit

Permalink
Example trivial implementation of C23 [[safe]] attribute support
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
  • Loading branch information
ojeda committed Feb 5, 2021
1 parent 43ff75f commit c5d9117
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion clang/include/clang-c/Index.h
Original file line number Diff line number Diff line change
Expand Up @@ -2627,7 +2627,8 @@ enum CXCursorKind {
CXCursor_WarnUnusedAttr = 439,
CXCursor_WarnUnusedResultAttr = 440,
CXCursor_AlignedAttr = 441,
CXCursor_LastAttr = CXCursor_AlignedAttr,
CXCursor_SafeAttr = 442,
CXCursor_LastAttr = CXCursor_SafeAttr,

/* Preprocessing */
CXCursor_PreprocessingDirective = 500,
Expand Down
6 changes: 6 additions & 0 deletions clang/include/clang/Basic/Attr.td
Original file line number Diff line number Diff line change
Expand Up @@ -2422,6 +2422,12 @@ def ObjCRequiresPropertyDefs : InheritableAttr {
let SimpleHandler = 1;
}

def Safe : InheritableAttr {
let Spellings = [C2x<"", "safe">];
let Subjects = SubjectList<[Function]>;
let Documentation = [Undocumented];
}

def Unused : InheritableAttr {
let Spellings = [CXX11<"", "maybe_unused", 201603>, GCC<"unused">,
C2x<"", "maybe_unused">];
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -8212,6 +8212,8 @@ def ext_cxx17_attr : Extension<
"use of the %0 attribute is a C++17 extension">, InGroup<CXX17>;
def ext_cxx20_attr : Extension<
"use of the %0 attribute is a C++20 extension">, InGroup<CXX20>;
def ext_c2x_attr : Extension<
"use of the %0 attribute is a C2x extension">, InGroup<C2x>;

def warn_unused_comparison : Warning<
"%select{equality|inequality|relational|three-way}0 comparison result unused">,
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Parse/ParseDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4018,6 +4018,7 @@ static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
case ParsedAttr::AT_FallThrough:
case ParsedAttr::AT_CXX11NoReturn:
case ParsedAttr::AT_NoUniqueAddress:
case ParsedAttr::AT_Safe:
return true;
case ParsedAttr::AT_WarnUnusedResult:
return !ScopeName && AttrName->getName().equals("nodiscard");
Expand Down
12 changes: 12 additions & 0 deletions clang/lib/Sema/SemaDeclAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2153,6 +2153,15 @@ static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
D->addAttr(::new (S.Context) CarriesDependencyAttr(S.Context, AL));
}

static void handleSafeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
// If this is spelled as the standard C2x attribute, but not in C2x, warn
// about using it as an extension.
if (!S.getLangOpts().C2x && AL.isC2xAttribute())
S.Diag(AL.getLoc(), diag::ext_c2x_attr) << AL;

D->addAttr(::new (S.Context) SafeAttr(S.Context, AL));
}

static void handleUnusedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
bool IsCXX17Attr = AL.isCXX11Attribute() && !AL.getScopeName();

Expand Down Expand Up @@ -7196,6 +7205,9 @@ static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
case ParsedAttr::AT_Unused:
handleUnusedAttr(S, D, AL);
break;
case ParsedAttr::AT_Safe:
handleSafeAttr(S, D, AL);
break;
case ParsedAttr::AT_NotTailCalled:
handleSimpleAttributeWithExclusions<NotTailCalledAttr, AlwaysInlineAttr>(
S, D, AL);
Expand Down
2 changes: 2 additions & 0 deletions clang/tools/libclang/CIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5637,6 +5637,8 @@ CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
return cxstring::createRef("FriendDecl");
case CXCursor_ConvergentAttr:
return cxstring::createRef("attribute(convergent)");
case CXCursor_SafeAttr:
return cxstring::createRef("attribute(safe)");
case CXCursor_WarnUnusedAttr:
return cxstring::createRef("attribute(warn_unused)");
case CXCursor_WarnUnusedResultAttr:
Expand Down
2 changes: 2 additions & 0 deletions clang/tools/libclang/CXCursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ static CXCursorKind GetCursorKind(const Attr *A) {
return CXCursor_FlagEnum;
case attr::Convergent:
return CXCursor_ConvergentAttr;
case attr::Safe:
return CXCursor_SafeAttr;
case attr::WarnUnused:
return CXCursor_WarnUnusedAttr;
case attr::WarnUnusedResult:
Expand Down

0 comments on commit c5d9117

Please sign in to comment.