[clangd] Do not drop diagnostics from macros
authorIlya Biryukov <ibiryukov@google.com>
Mon, 26 Nov 2018 17:05:13 +0000 (17:05 +0000)
committerIlya Biryukov <ibiryukov@google.com>
Mon, 26 Nov 2018 17:05:13 +0000 (17:05 +0000)
if they still end up being in the main file.

llvm-svn: 347574

clang-tools-extra/clangd/Diagnostics.cpp
clang-tools-extra/unittests/clangd/ClangdUnitTests.cpp

index 9a330f8..a8bfa29 100644 (file)
@@ -79,7 +79,7 @@ Range diagnosticRange(const clang::Diagnostic &D, const LangOptions &L) {
 }
 
 bool isInsideMainFile(const SourceLocation Loc, const SourceManager &M) {
-  return Loc.isValid() && M.isWrittenInMainFile(Loc);
+  return Loc.isValid() && M.isWrittenInMainFile(M.getFileLoc(Loc));
 }
 
 bool isInsideMainFile(const clang::Diagnostic &D) {
index 687f917..39c8f0c 100644 (file)
@@ -159,7 +159,9 @@ TEST(DiagnosticsTest, ClangTidy) {
                    "macro expansion [bugprone-macro-repeated-side-effects]"),
               WithNote(Diag(Test.range("macrodef"),
                             "macro 'SQUARE' defined here "
-                            "[bugprone-macro-repeated-side-effects]")))));
+                            "[bugprone-macro-repeated-side-effects]"))),
+          Diag(Test.range("macroarg"),
+               "multiple unsequenced modifications to 'y'")));
 }
 
 TEST(DiagnosticsTest, Preprocessor) {
@@ -181,6 +183,27 @@ TEST(DiagnosticsTest, Preprocessor) {
       ElementsAre(Diag(Test.range(), "use of undeclared identifier 'b'")));
 }
 
+TEST(DiagnosticsTest, InsideMacros) {
+  Annotations Test(R"cpp(
+    #define TEN 10
+    #define RET(x) return x + 10
+
+    int* foo() {
+      RET($foo[[0]]);
+    }
+    int* bar() {
+      return $bar[[TEN]];
+    }
+    )cpp");
+  EXPECT_THAT(TestTU::withCode(Test.code()).build().getDiagnostics(),
+              ElementsAre(Diag(Test.range("foo"),
+                               "cannot initialize return object of type "
+                               "'int *' with an rvalue of type 'int'"),
+                          Diag(Test.range("bar"),
+                               "cannot initialize return object of type "
+                               "'int *' with an rvalue of type 'int'")));
+}
+
 TEST(DiagnosticsTest, ToLSP) {
   clangd::Diag D;
   D.Message = "something terrible happened";