[clang-tools-extra] a46bbc1 - [cland] Dont emit missing newline warnings when building preamble
Kadir Cetinkaya via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 23 00:02:05 PDT 2021
Author: Kadir Cetinkaya
Date: 2021-04-23T08:56:32+02:00
New Revision: a46bbc14f004c06bd1317e6d7a486c29133f1c0b
URL: https://github.com/llvm/llvm-project/commit/a46bbc14f004c06bd1317e6d7a486c29133f1c0b
DIFF: https://github.com/llvm/llvm-project/commit/a46bbc14f004c06bd1317e6d7a486c29133f1c0b.diff
LOG: [cland] Dont emit missing newline warnings when building preamble
When building preamble, clangd truncates file contents. This yielded
errnous warnings in some cases.
This patch fixes the issue by turning off no-newline-at-eof warnings whenever
the file has more contents than the preamble.
Fixes https://github.com/clangd/clangd/issues/744.
Differential Revision: https://reviews.llvm.org/D100501
Added:
Modified:
clang-tools-extra/clangd/Preamble.cpp
clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/Preamble.cpp b/clang-tools-extra/clangd/Preamble.cpp
index 73b1c900cfa5a..e003af6e0dfa5 100644
--- a/clang-tools-extra/clangd/Preamble.cpp
+++ b/clang-tools-extra/clangd/Preamble.cpp
@@ -15,6 +15,7 @@
#include "support/Trace.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/Basic/Diagnostic.h"
+#include "clang/Basic/DiagnosticLex.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
@@ -341,6 +342,20 @@ buildPreamble(PathRef FileName, CompilerInvocation CI,
llvm::IntrusiveRefCntPtr<DiagnosticsEngine> PreambleDiagsEngine =
CompilerInstance::createDiagnostics(&CI.getDiagnosticOpts(),
&PreambleDiagnostics, false);
+ PreambleDiagnostics.setLevelAdjuster(
+ [&](DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info) {
+ switch (Info.getID()) {
+ case diag::warn_no_newline_eof:
+ case diag::warn_cxx98_compat_no_newline_eof:
+ case diag::ext_no_newline_eof:
+ // If the preamble doesn't span the whole file, drop the no newline at
+ // eof warnings.
+ return Bounds.Size != ContentsBuffer->getBufferSize()
+ ? DiagnosticsEngine::Level::Ignored
+ : DiagLevel;
+ }
+ return DiagLevel;
+ });
// Skip function bodies when building the preamble to speed up building
// the preamble and make it smaller.
diff --git a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
index d5096cba7e740..4aa9cb7d66105 100644
--- a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
@@ -1373,6 +1373,25 @@ TEST(ParsedASTTest, ModuleSawDiag) {
EXPECT_THAT(*AST.getDiagnostics(),
testing::Contains(Diag(Code.range(), KDiagMsg.str())));
}
+
+TEST(Preamble, EndsOnNonEmptyLine) {
+ TestTU TU;
+ TU.ExtraArgs = {"-Wnewline-eof"};
+
+ {
+ TU.Code = "#define FOO\n void bar();\n";
+ auto AST = TU.build();
+ EXPECT_THAT(*AST.getDiagnostics(), IsEmpty());
+ }
+ {
+ Annotations Code("#define FOO[[]]");
+ TU.Code = Code.code().str();
+ auto AST = TU.build();
+ EXPECT_THAT(
+ *AST.getDiagnostics(),
+ testing::Contains(Diag(Code.range(), "no newline at end of file")));
+ }
+}
} // namespace
} // namespace clangd
} // namespace clang
More information about the cfe-commits
mailing list