njames93 created this revision. njames93 added a reviewer: rsmith. Herald added a project: clang. Herald added a subscriber: cfe-commits. njames93 requested review of this revision.
If a static assert has a message as the right side of an and condition, suggest a fix it of replacing the '&&' to ','. `static_assert(cond && "Failed Cond")` -> `static_assert(cond, "Failed cond")` This use case comes up when lazily replacing asserts with static asserts. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D89065 Files: clang/lib/Parse/ParseDeclCXX.cpp Index: clang/lib/Parse/ParseDeclCXX.cpp =================================================================== --- clang/lib/Parse/ParseDeclCXX.cpp +++ clang/lib/Parse/ParseDeclCXX.cpp @@ -10,15 +10,16 @@ // //===----------------------------------------------------------------------===// -#include "clang/Parse/Parser.h" #include "clang/AST/ASTContext.h" #include "clang/AST/DeclTemplate.h" #include "clang/AST/PrettyDeclStackTrace.h" #include "clang/Basic/Attributes.h" #include "clang/Basic/CharInfo.h" +#include "clang/Basic/DiagnosticParse.h" #include "clang/Basic/OperatorKinds.h" #include "clang/Basic/TargetInfo.h" #include "clang/Parse/ParseDiagnostic.h" +#include "clang/Parse/Parser.h" #include "clang/Parse/RAIIObjectsForParser.h" #include "clang/Sema/DeclSpec.h" #include "clang/Sema/ParsedTemplate.h" @@ -858,6 +859,16 @@ DeclFromDeclSpec); } +static FixItHint getStaticAssertNoMessageFixIt(const Expr *AssertExpr, + SourceLocation EndExprLoc) { + if (const auto *BO = llvm::dyn_cast_or_null<BinaryOperator>(AssertExpr)) { + if (BO->getOpcode() == BO_LAnd && + llvm::isa_and_nonnull<StringLiteral>(BO->getRHS())) + return FixItHint::CreateReplacement(BO->getOperatorLoc(), ","); + } + return FixItHint::CreateInsertion(EndExprLoc, ", \"\""); +} + /// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration. /// /// [C++0x] static_assert-declaration: @@ -894,12 +905,11 @@ ExprResult AssertMessage; if (Tok.is(tok::r_paren)) { - Diag(Tok, getLangOpts().CPlusPlus17 - ? diag::warn_cxx14_compat_static_assert_no_message - : diag::ext_static_assert_no_message) - << (getLangOpts().CPlusPlus17 - ? FixItHint() - : FixItHint::CreateInsertion(Tok.getLocation(), ", \"\"")); + if (getLangOpts().CPlusPlus17) + Diag(Tok, diag::warn_cxx14_compat_static_assert_no_message); + else + Diag(Tok, diag::ext_static_assert_no_message) + << getStaticAssertNoMessageFixIt(AssertExpr.get(), Tok.getLocation()); } else { if (ExpectAndConsume(tok::comma)) { SkipUntil(tok::semi);
Index: clang/lib/Parse/ParseDeclCXX.cpp =================================================================== --- clang/lib/Parse/ParseDeclCXX.cpp +++ clang/lib/Parse/ParseDeclCXX.cpp @@ -10,15 +10,16 @@ // //===----------------------------------------------------------------------===// -#include "clang/Parse/Parser.h" #include "clang/AST/ASTContext.h" #include "clang/AST/DeclTemplate.h" #include "clang/AST/PrettyDeclStackTrace.h" #include "clang/Basic/Attributes.h" #include "clang/Basic/CharInfo.h" +#include "clang/Basic/DiagnosticParse.h" #include "clang/Basic/OperatorKinds.h" #include "clang/Basic/TargetInfo.h" #include "clang/Parse/ParseDiagnostic.h" +#include "clang/Parse/Parser.h" #include "clang/Parse/RAIIObjectsForParser.h" #include "clang/Sema/DeclSpec.h" #include "clang/Sema/ParsedTemplate.h" @@ -858,6 +859,16 @@ DeclFromDeclSpec); } +static FixItHint getStaticAssertNoMessageFixIt(const Expr *AssertExpr, + SourceLocation EndExprLoc) { + if (const auto *BO = llvm::dyn_cast_or_null<BinaryOperator>(AssertExpr)) { + if (BO->getOpcode() == BO_LAnd && + llvm::isa_and_nonnull<StringLiteral>(BO->getRHS())) + return FixItHint::CreateReplacement(BO->getOperatorLoc(), ","); + } + return FixItHint::CreateInsertion(EndExprLoc, ", \"\""); +} + /// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration. /// /// [C++0x] static_assert-declaration: @@ -894,12 +905,11 @@ ExprResult AssertMessage; if (Tok.is(tok::r_paren)) { - Diag(Tok, getLangOpts().CPlusPlus17 - ? diag::warn_cxx14_compat_static_assert_no_message - : diag::ext_static_assert_no_message) - << (getLangOpts().CPlusPlus17 - ? FixItHint() - : FixItHint::CreateInsertion(Tok.getLocation(), ", \"\"")); + if (getLangOpts().CPlusPlus17) + Diag(Tok, diag::warn_cxx14_compat_static_assert_no_message); + else + Diag(Tok, diag::ext_static_assert_no_message) + << getStaticAssertNoMessageFixIt(AssertExpr.get(), Tok.getLocation()); } else { if (ExpectAndConsume(tok::comma)) { SkipUntil(tok::semi);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits