diff options
author | zebullax <zebullax@gmail.com> | 2025-07-14 22:54:24 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-07-14 09:54:24 -0400 |
commit | e35b01da11ade0f89c0dafb8e723af3ae8be5c01 (patch) | |
tree | 93052ede9d1645cdcbc68664f24315d8f51a3549 /clang/lib/Sema | |
parent | ea8ff792e33bb3ff3c8c6e79b8bf47cb0d7761b4 (diff) | |
download | llvm-e35b01da11ade0f89c0dafb8e723af3ae8be5c01.zip llvm-e35b01da11ade0f89c0dafb8e723af3ae8be5c01.tar.gz llvm-e35b01da11ade0f89c0dafb8e723af3ae8be5c01.tar.bz2 |
[clang] Build argument string for clang::warn_unused_result (#148090)
Preserve the argument-clause for `warn-unused-result` when under clang::
scope.
We are not touching gnu:: scope for now as it's an error for GCC to have
that string. Personally I think it would be ok to relax it here too as
we are not introducing breakage to currently passing code, but feedback
is to go slowly about it.
Diffstat (limited to 'clang/lib/Sema')
-rw-r--r-- | clang/lib/Sema/SemaDeclAttr.cpp | 51 |
1 files changed, 30 insertions, 21 deletions
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp index 60a9aee..5f481ed1 100644 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -2902,28 +2902,37 @@ static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) { } StringRef Str; - if (AL.isStandardAttributeSyntax() && !AL.getScopeName()) { - // The standard attribute cannot be applied to variable declarations such - // as a function pointer. - if (isa<VarDecl>(D)) - S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) - << AL << AL.isRegularKeywordAttribute() - << ExpectedFunctionOrClassOrEnum; - - // If this is spelled as the standard C++17 attribute, but not in C++17, - // warn about using it as an extension. If there are attribute arguments, - // then claim it's a C++20 extension instead. C23 supports this attribute - // with the message; no extension warning is needed there beyond the one - // already issued for accepting attributes in older modes. - const LangOptions &LO = S.getLangOpts(); - if (AL.getNumArgs() == 1) { - if (LO.CPlusPlus && !LO.CPlusPlus20) - S.Diag(AL.getLoc(), diag::ext_cxx20_attr) << AL; - - if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, nullptr)) + if (AL.isStandardAttributeSyntax()) { + // If this is spelled [[clang::warn_unused_result]] we look for an optional + // string literal. This is not gated behind any specific version of the + // standard. + if (AL.isClangScope()) { + if (AL.getNumArgs() == 1 && + !S.checkStringLiteralArgumentAttr(AL, 0, Str, nullptr)) return; - } else if (LO.CPlusPlus && !LO.CPlusPlus17) - S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL; + } else if (!AL.getScopeName()) { + // The standard attribute cannot be applied to variable declarations such + // as a function pointer. + if (isa<VarDecl>(D)) + S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) + << AL << AL.isRegularKeywordAttribute() + << ExpectedFunctionOrClassOrEnum; + + // If this is spelled as the standard C++17 attribute, but not in C++17, + // warn about using it as an extension. If there are attribute arguments, + // then claim it's a C++20 extension instead. C23 supports this attribute + // with the message; no extension warning is needed there beyond the one + // already issued for accepting attributes in older modes. + const LangOptions &LO = S.getLangOpts(); + if (AL.getNumArgs() == 1) { + if (LO.CPlusPlus && !LO.CPlusPlus20) + S.Diag(AL.getLoc(), diag::ext_cxx20_attr) << AL; + + if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, nullptr)) + return; + } else if (LO.CPlusPlus && !LO.CPlusPlus17) + S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL; + } } if ((!AL.isGNUAttribute() && |