hokein created this revision.
hokein added a reviewer: kadircet.
Herald added a subscriber: arphaman.
Herald added a project: All.
hokein requested review of this revision.
Herald added subscribers: MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

Add a config as a filed in TestTU, for each TestTU::build, we set the
config. And make the Config copyable.

This is needed for enabling the unused-include feature by default.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D147227

Files:
  clang-tools-extra/clangd/Config.h
  clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
  clang-tools-extra/clangd/unittests/TestTU.cpp
  clang-tools-extra/clangd/unittests/TestTU.h

Index: clang-tools-extra/clangd/unittests/TestTU.h
===================================================================
--- clang-tools-extra/clangd/unittests/TestTU.h
+++ clang-tools-extra/clangd/unittests/TestTU.h
@@ -19,6 +19,7 @@
 
 #include "../TidyProvider.h"
 #include "Compiler.h"
+#include "Config.h"
 #include "FeatureModule.h"
 #include "ParsedAST.h"
 #include "TestFS.h"
@@ -45,6 +46,8 @@
     return TU;
   }
 
+  TestTU();
+
   // The code to be compiled.
   std::string Code;
   std::string Filename = "TestTU.cpp";
@@ -59,6 +62,8 @@
   // Extra arguments for the compiler invocation.
   std::vector<std::string> ExtraArgs;
 
+  Config Cfg;
+
   // Predefine macros such as __UINTPTR_TYPE__.
   bool PredefineMacros = false;
 
Index: clang-tools-extra/clangd/unittests/TestTU.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/TestTU.cpp
+++ clang-tools-extra/clangd/unittests/TestTU.cpp
@@ -9,6 +9,7 @@
 #include "TestTU.h"
 #include "CompileCommands.h"
 #include "Compiler.h"
+#include "Config.h"
 #include "Diagnostics.h"
 #include "TestFS.h"
 #include "index/FileIndex.h"
@@ -23,6 +24,12 @@
 namespace clang {
 namespace clangd {
 
+TestTU::TestTU() {
+  // Disable the clangd-specific diagnostics by default.
+  Cfg.Diagnostics.UnusedIncludes = Config::IncludesPolicy::None;
+  Cfg.Diagnostics.MissingIncludes = Config::IncludesPolicy::None;
+}
+
 ParseInputs TestTU::inputs(MockFS &FS) const {
   std::string FullFilename = testPath(Filename),
               FullHeaderName = testPath(HeaderFilename),
@@ -122,7 +129,7 @@
     initializeModuleCache(*CI);
   auto ModuleCacheDeleter = llvm::make_scope_exit(
       std::bind(deleteModuleCache, CI->getHeaderSearchOpts().ModuleCachePath));
-
+  WithContextValue Ctx(Config::Key, Cfg);
   auto Preamble = clang::clangd::buildPreamble(testPath(Filename), *CI, Inputs,
                                                /*StoreInMemory=*/true,
                                                /*PreambleCallback=*/nullptr);
Index: clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
+++ clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
@@ -473,9 +473,8 @@
       ElementsAre(Diag(Main.range(), "use of undeclared identifier 'unknown'"),
                   Diag(Main.range("ret"),
                        "void function 'x' should not return a value")));
-  Config Cfg;
-  Cfg.Diagnostics.Suppress.insert("return-type");
-  WithContextValue WithCfg(Config::Key, std::move(Cfg));
+
+  TU.Cfg.Diagnostics.Suppress.insert("return-type");
   EXPECT_THAT(*TU.build().getDiagnostics(),
               ElementsAre(Diag(Main.range(),
                                "use of undeclared identifier 'unknown'")));
@@ -490,9 +489,7 @@
   )cpp");
   auto TU = TestTU::withCode(Main.code());
   TU.AdditionalFiles["header.hpp"] = std::string(Header.code());
-  Config Cfg;
-  Cfg.Diagnostics.Suppress.insert("init_conversion_failed");
-  WithContextValue WithCfg(Config::Key, std::move(Cfg));
+  TU.Cfg.Diagnostics.Suppress.insert("init_conversion_failed");
   EXPECT_THAT(*TU.build().getDiagnostics(), IsEmpty());
 }
 
@@ -1899,12 +1896,10 @@
   TU.ExtraArgs = {"-isystem" + testPath("system")};
   // Off by default.
   EXPECT_THAT(*TU.build().getDiagnostics(), IsEmpty());
-  Config Cfg;
-  Cfg.Diagnostics.UnusedIncludes = Config::IncludesPolicy::Strict;
+  TU.Cfg.Diagnostics.UnusedIncludes = Config::IncludesPolicy::Strict;
   // Set filtering.
-  Cfg.Diagnostics.Includes.IgnoreHeader.emplace_back(
+  TU.Cfg.Diagnostics.Includes.IgnoreHeader.emplace_back(
       [](llvm::StringRef Header) { return Header.endswith("ignore.h"); });
-  WithContextValue WithCfg(Config::Key, std::move(Cfg));
   auto AST = TU.build();
   EXPECT_THAT(
       *AST.getDiagnostics(),
@@ -1916,12 +1911,10 @@
   auto &Diag = AST.getDiagnostics()->front();
   EXPECT_EQ(getDiagnosticDocURI(Diag.Source, Diag.ID, Diag.Name),
             std::string("https://clangd.llvm.org/guides/include-cleaner";));
-  Cfg.Diagnostics.SuppressAll = true;
-  WithContextValue SuppressAllWithCfg(Config::Key, std::move(Cfg));
+  TU.Cfg.Diagnostics.SuppressAll = true;
   EXPECT_THAT(*TU.build().getDiagnostics(), IsEmpty());
-  Cfg.Diagnostics.SuppressAll = false;
-  Cfg.Diagnostics.Suppress = {"unused-includes"};
-  WithContextValue SuppressFilterWithCfg(Config::Key, std::move(Cfg));
+  TU.Cfg.Diagnostics.SuppressAll = false;
+  TU.Cfg.Diagnostics.Suppress = {"unused-includes"};
   EXPECT_THAT(*TU.build().getDiagnostics(), IsEmpty());
 }
 
Index: clang-tools-extra/clangd/Config.h
===================================================================
--- clang-tools-extra/clangd/Config.h
+++ clang-tools-extra/clangd/Config.h
@@ -25,7 +25,6 @@
 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CONFIG_H
 
 #include "support/Context.h"
-#include "llvm/ADT/FunctionExtras.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringSet.h"
 #include <functional>
@@ -48,8 +47,8 @@
   static clangd::Key<Config> Key;
 
   Config() = default;
-  Config(const Config &) = delete;
-  Config &operator=(const Config &) = delete;
+  Config(const Config &) = default;
+  Config &operator=(const Config &) = default;
   Config(Config &&) = default;
   Config &operator=(Config &&) = default;
 
@@ -62,8 +61,7 @@
   /// Controls how the compile command for the current file is determined.
   struct {
     /// Edits to apply to the compile command, in sequence.
-    std::vector<llvm::unique_function<void(std::vector<std::string> &) const>>
-        Edits;
+    std::vector<std::function<void(std::vector<std::string> &)>> Edits;
     /// Where to search for compilation databases for this file's flags.
     CDBSearchSpec CDBSearch = {CDBSearchSpec::Ancestors, std::nullopt};
   } CompileFlags;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to