njames93 updated this revision to Diff 325284. njames93 added a comment. Add tests for fixits.
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D89065/new/ https://reviews.llvm.org/D89065 Files: clang/lib/Parse/ParseDeclCXX.cpp clang/test/FixIt/fixit-static-assert.cpp Index: clang/test/FixIt/fixit-static-assert.cpp =================================================================== --- /dev/null +++ clang/test/FixIt/fixit-static-assert.cpp @@ -0,0 +1,26 @@ +// RUN: %clang_cc1 -std=c++14 %s -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s +// Ensure no warnings are emitted in c++17. +// RUN: %clang_cc1 -std=c++17 %s -Werror -fsyntax-only + +static_assert(true && "String"); +// CHECK-DAG: {[[@LINE-1]]:20-[[@LINE-1]]:22}:"," + +// String literal prefixes are good. +static_assert(true && R"(RawString)"); +// CHECK-DAG: {[[@LINE-1]]:20-[[@LINE-1]]:22}:"," +static_assert(true && L"RawString"); +// CHECK-DAG: {[[@LINE-1]]:20-[[@LINE-1]]:22}:"," + + +static_assert(true); +// CHECK-DAG: {[[@LINE-1]]:19-[[@LINE-1]]:19}:", \"\"" + +// While its technically possible to transform this to +// static_assert(true, "String") we don't attempt this fix. +static_assert("String" && true); +// CHECK-DAG: {[[@LINE-1]]:31-[[@LINE-1]]:31}:", \"\"" + +// Don't be smart and look in parentheses. +static_assert((true && "String")); +// CHECK-DAG: {[[@LINE-1]]:33-[[@LINE-1]]:33}:", \"\"" + Index: clang/lib/Parse/ParseDeclCXX.cpp =================================================================== --- clang/lib/Parse/ParseDeclCXX.cpp +++ clang/lib/Parse/ParseDeclCXX.cpp @@ -856,6 +856,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<StringLiteral>(BO->getRHS()->IgnoreImpCasts())) + return FixItHint::CreateReplacement(BO->getOperatorLoc(), ","); + } + return FixItHint::CreateInsertion(EndExprLoc, ", \"\""); +} + /// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration. /// /// [C++0x] static_assert-declaration: @@ -892,12 +902,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/test/FixIt/fixit-static-assert.cpp =================================================================== --- /dev/null +++ clang/test/FixIt/fixit-static-assert.cpp @@ -0,0 +1,26 @@ +// RUN: %clang_cc1 -std=c++14 %s -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s +// Ensure no warnings are emitted in c++17. +// RUN: %clang_cc1 -std=c++17 %s -Werror -fsyntax-only + +static_assert(true && "String"); +// CHECK-DAG: {[[@LINE-1]]:20-[[@LINE-1]]:22}:"," + +// String literal prefixes are good. +static_assert(true && R"(RawString)"); +// CHECK-DAG: {[[@LINE-1]]:20-[[@LINE-1]]:22}:"," +static_assert(true && L"RawString"); +// CHECK-DAG: {[[@LINE-1]]:20-[[@LINE-1]]:22}:"," + + +static_assert(true); +// CHECK-DAG: {[[@LINE-1]]:19-[[@LINE-1]]:19}:", \"\"" + +// While its technically possible to transform this to +// static_assert(true, "String") we don't attempt this fix. +static_assert("String" && true); +// CHECK-DAG: {[[@LINE-1]]:31-[[@LINE-1]]:31}:", \"\"" + +// Don't be smart and look in parentheses. +static_assert((true && "String")); +// CHECK-DAG: {[[@LINE-1]]:33-[[@LINE-1]]:33}:", \"\"" + Index: clang/lib/Parse/ParseDeclCXX.cpp =================================================================== --- clang/lib/Parse/ParseDeclCXX.cpp +++ clang/lib/Parse/ParseDeclCXX.cpp @@ -856,6 +856,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<StringLiteral>(BO->getRHS()->IgnoreImpCasts())) + return FixItHint::CreateReplacement(BO->getOperatorLoc(), ","); + } + return FixItHint::CreateInsertion(EndExprLoc, ", \"\""); +} + /// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration. /// /// [C++0x] static_assert-declaration: @@ -892,12 +902,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