kadircet updated this revision to Diff 339901. kadircet marked an inline comment as done. kadircet added a comment. Herald added a subscriber: cfe-commits.
- Handle cxx98 and extension versions of the warning too. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D100501/new/ https://reviews.llvm.org/D100501 Files: clang-tools-extra/clangd/Preamble.cpp clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp Index: clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp +++ clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp @@ -1373,6 +1373,25 @@ 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 Index: clang-tools-extra/clangd/Preamble.cpp =================================================================== --- clang-tools-extra/clangd/Preamble.cpp +++ 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 @@ 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.
Index: clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp +++ clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp @@ -1373,6 +1373,25 @@ 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 Index: clang-tools-extra/clangd/Preamble.cpp =================================================================== --- clang-tools-extra/clangd/Preamble.cpp +++ 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 @@ 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.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits