From: Kirill Bobyrev Date: Wed, 27 Oct 2021 09:50:35 +0000 (+0200) Subject: [clangd] IncludeCleaner: Don't warn on system headers X-Git-Tag: upstream/15.0.7~27524 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c4723785c1902d6a53f3808fa1dabb2a97c1cc6e;p=platform%2Fupstream%2Fllvm.git [clangd] IncludeCleaner: Don't warn on system headers This is a temporary hack to disable diagnostics for system headers. As of right now, IncludeCleaner does not handle the Standard Library correctly and will report most system headers as unused because very few symbols are defined in top-level system headers. This will eventually be fixed, but for now we are aiming for the most conservative approach with as little false-positive warnings as possible. After the initial prototype and core functionality is polished, I will turn back to handling the Standard Library as it requires custom logic. Reviewed By: sammccall Differential Revision: https://reviews.llvm.org/D112571 --- diff --git a/clang-tools-extra/clangd/IncludeCleaner.cpp b/clang-tools-extra/clangd/IncludeCleaner.cpp index 95b4b24..cc69fda 100644 --- a/clang-tools-extra/clangd/IncludeCleaner.cpp +++ b/clang-tools-extra/clangd/IncludeCleaner.cpp @@ -196,6 +196,14 @@ void findReferencedMacros(ParsedAST &AST, ReferencedLocations &Result) { } } +// FIXME(kirillbobyrev): We currently do not support the umbrella headers. +// Standard Library headers are typically umbrella headers, and system headers +// are likely to be the Standard Library headers. Until we have a good support +// for umbrella headers and Standard Library headers, don't warn about them. +bool mayConsiderUnused(const Inclusion *Inc) { + return Inc->Written.front() != '<'; +} + } // namespace ReferencedLocations findReferencedLocations(ParsedAST &AST) { @@ -283,6 +291,8 @@ std::vector issueUnusedIncludesDiagnostics(ParsedAST &AST, ->getName() .str(); for (const auto *Inc : computeUnusedIncludes(AST)) { + if (!mayConsiderUnused(Inc)) + continue; Diag D; D.Message = llvm::formatv("included header {0} is not used", diff --git a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp index c3c56bd..4d26968 100644 --- a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp +++ b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp @@ -1483,7 +1483,9 @@ TEST(Diagnostics, Tags) { TEST(DiagnosticsTest, IncludeCleaner) { Annotations Test(R"cpp( $fix[[ $diag[[#include "unused.h"]] -]] #include "used.h" +]]#include "used.h" + + #include void foo() { used(); @@ -1497,6 +1499,8 @@ $fix[[ $diag[[#include "unused.h"]] TU.AdditionalFiles["used.h"] = R"cpp( void used() {} )cpp"; + TU.AdditionalFiles["system/system_header.h"] = ""; + TU.ExtraArgs = {"-isystem" + testPath("system")}; // Off by default. EXPECT_THAT(*TU.build().getDiagnostics(), IsEmpty()); Config Cfg; diff --git a/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp b/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp index 38cd8da..aeb76f8 100644 --- a/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp +++ b/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp @@ -206,6 +206,7 @@ TEST(IncludeCleaner, GetUnusedHeaders) { #include "dir/c.h" #include "dir/unused.h" #include "unused.h" + #include void foo() { a(); b(); @@ -220,17 +221,17 @@ TEST(IncludeCleaner, GetUnusedHeaders) { TU.AdditionalFiles["dir/c.h"] = "void c();"; TU.AdditionalFiles["unused.h"] = "void unused();"; TU.AdditionalFiles["dir/unused.h"] = "void dirUnused();"; - TU.AdditionalFiles["not_included.h"] = "void notIncluded();"; - TU.ExtraArgs = {"-I" + testPath("dir")}; + TU.AdditionalFiles["system/system_header.h"] = ""; + TU.ExtraArgs.push_back("-I" + testPath("dir")); + TU.ExtraArgs.push_back("-isystem" + testPath("system")); TU.Code = MainFile.str(); ParsedAST AST = TU.build(); - auto UnusedIncludes = computeUnusedIncludes(AST); - std::vector UnusedHeaders; - UnusedHeaders.reserve(UnusedIncludes.size()); - for (const auto &Include : UnusedIncludes) - UnusedHeaders.push_back(Include->Written); - EXPECT_THAT(UnusedHeaders, - UnorderedElementsAre("\"unused.h\"", "\"dir/unused.h\"")); + std::vector UnusedIncludes; + for (const auto &Include : computeUnusedIncludes(AST)) + UnusedIncludes.push_back(Include->Written); + EXPECT_THAT(UnusedIncludes, + UnorderedElementsAre("\"unused.h\"", "\"dir/unused.h\"", + "")); } TEST(IncludeCleaner, ScratchBuffer) {