[clang-tools-extra] f0e60f9 - [clangd] Suppress clang-tidy warnings for code spelled in system macros

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 11 07:11:33 PST 2023


Author: Sam McCall
Date: 2023-01-11T16:11:25+01:00
New Revision: f0e60f99aefff66ca6223eb3c3a36615f36454ec

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

LOG: [clangd] Suppress clang-tidy warnings for code spelled in system macros

This aligns with the default behavior of clang-tidy (which we offer no
way to override).

Fixes https://github.com/clangd/clangd/issues/1448

Differential Revision: https://reviews.llvm.org/D141495

Added: 
    

Modified: 
    clang-tools-extra/clangd/ParsedAST.cpp
    clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/ParsedAST.cpp b/clang-tools-extra/clangd/ParsedAST.cpp
index b8dd161649fdb..637cadaf86c64 100644
--- a/clang-tools-extra/clangd/ParsedAST.cpp
+++ b/clang-tools-extra/clangd/ParsedAST.cpp
@@ -11,7 +11,6 @@
 #include "../clang-tidy/ClangTidyDiagnosticConsumer.h"
 #include "../clang-tidy/ClangTidyModuleRegistry.h"
 #include "AST.h"
-#include "ASTSignals.h"
 #include "Compiler.h"
 #include "Config.h"
 #include "Diagnostics.h"
@@ -543,6 +542,10 @@ ParsedAST::build(llvm::StringRef Filename, const ParseInputs &Inputs,
             // NOLINT comments)?
             return DiagnosticsEngine::Ignored;
           }
+          // Match behavior for clang-tidy --system-headers=0 (the default).
+          if (Info.hasSourceManager() &&
+              Info.getSourceManager().isInSystemMacro(Info.getLocation()))
+            return DiagnosticsEngine::Ignored;
 
           // Check for warning-as-error.
           if (DiagLevel == DiagnosticsEngine::Warning &&

diff  --git a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
index 7310a11dc632f..824756a970211 100644
--- a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
@@ -527,6 +527,37 @@ TEST(DiagnosticTest, ClangTidySuppressionComment) {
           diagName("bugprone-integer-division")))));
 }
 
+TEST(DiagnosticTest, ClangTidySystemMacro) {
+  Annotations Main(R"cpp(
+    #include "user.h"
+    #include "system.h"
+    int i = 3;
+    double x = $inline[[8]] / i;
+    double y = $user[[DIVIDE_USER]](i);
+    double z = DIVIDE_SYS(i);
+  )cpp");
+
+  auto TU = TestTU::withCode(Main.code());
+  TU.AdditionalFiles["user.h"] = R"cpp(
+    #define DIVIDE_USER(Y) 8/Y
+  )cpp";
+  TU.AdditionalFiles["system.h"] = R"cpp(
+    #pragma clang system_header
+    #define DIVIDE_SYS(Y) 8/Y
+  )cpp";
+
+  TU.ClangTidyProvider = addTidyChecks("bugprone-integer-division");
+  std::string BadDivision = "result of integer division used in a floating "
+                            "point context; possible loss of precision";
+
+  // Expect to see warning from user macros, but not system macros.
+  // This matches clang-tidy --system-headers=0 (the default).
+  EXPECT_THAT(*TU.build().getDiagnostics(),
+              ifTidyChecks(
+                  UnorderedElementsAre(Diag(Main.range("inline"), BadDivision),
+                                       Diag(Main.range("user"), BadDivision))));
+}
+
 TEST(DiagnosticTest, ClangTidyWarningAsError) {
   Annotations Main(R"cpp(
     int main() {


        


More information about the cfe-commits mailing list