[clang-tools-extra] 0cecb42 - [Sema] Include full range of the switch condition in -Wswitch diagnostic

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 10 13:51:00 PDT 2021


Author: Sam McCall
Date: 2021-08-10T22:50:40+02:00
New Revision: 0cecb42e4e13e1ce55a3568958ad104bbe6b6d6e

URL: https://github.com/llvm/llvm-project/commit/0cecb42e4e13e1ce55a3568958ad104bbe6b6d6e
DIFF: https://github.com/llvm/llvm-project/commit/0cecb42e4e13e1ce55a3568958ad104bbe6b6d6e.diff

LOG: [Sema] Include full range of the switch condition in -Wswitch diagnostic

Added: 
    

Modified: 
    clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
    clang/lib/Sema/SemaStmt.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
index 870402588e763..d4450b31e5f38 100644
--- a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
@@ -191,6 +191,26 @@ o]]();
                             "change 'fod' to 'foo'")))));
 }
 
+// Verify that the -Wswitch case-not-covered diagnostic range covers the
+// whole expression. This is important because the "populate-switch" tweak
+// fires for the full expression range (see tweaks/PopulateSwitchTests.cpp).
+// The quickfix flow only works end-to-end if the tweak can be triggered on
+// the diagnostic's range.
+TEST(DiagnosticsTest, WSwitch) {
+  Annotations Test(R"cpp(
+    enum A { X };
+    struct B { A a; };
+    void foo(B b) {
+      switch ([[b.a]]) {}
+    }
+  )cpp");
+  auto TU = TestTU::withCode(Test.code());
+  TU.ExtraArgs = {"-Wswitch"};
+  EXPECT_THAT(*TU.build().getDiagnostics(),
+              ElementsAre(Diag(Test.range(),
+                               "enumeration value 'X' not handled in switch")));
+}
+
 TEST(DiagnosticsTest, FlagsMatter) {
   Annotations Test("[[void]] main() {} // error-ok");
   auto TU = TestTU::withCode(Test.code());

diff  --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index 3baccec2d7bb4..f4f7e353a2c17 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -1563,7 +1563,7 @@ Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch,
         auto DB = Diag(CondExpr->getExprLoc(), TheDefaultStmt
                                                    ? diag::warn_def_missing_case
                                                    : diag::warn_missing_case)
-                  << (int)UnhandledNames.size();
+                  << CondExpr->getSourceRange() << (int)UnhandledNames.size();
 
         for (size_t I = 0, E = std::min(UnhandledNames.size(), (size_t)3);
              I != E; ++I)


        


More information about the cfe-commits mailing list