================
@@ -824,6 +828,86 @@ TEST(DiagnosticTest, ClangTidyEnablesClangWarning) {
   EXPECT_THAT(TU.build().getDiagnostics(), SizeIs(1));
 }
 
+TEST(DiagnosticTest, ClangTidyCompilerWarningsWithoutChecks) {
+  auto TU = TestTU::withCode("static void foo() {}");
+  TU.ClangTidyProvider = [](tidy::ClangTidyOptions &Opts, llvm::StringRef) {
+    Opts.Checks = "-*,clang-diagnostic-*";
+    Opts.ExtraArgs = {"-Wunused-function"};
+    Opts.WarningsAsErrors = "clang-diagnostic-unused-function";
+  };
+  auto Warning = AllOf(diagName("-Wunused-function"), diagSource(Diag::Clang),
+                       diagSeverity(DiagnosticsEngine::Warning));
+  EXPECT_THAT(TU.build().getDiagnostics(), ElementsAre(Warning));
+
+  // Without tidy checks, NOLINT and WarningsAsErrors don't affect warnings.
+  TU.Code = "static void foo() {} // NOLINT(clang-diagnostic-unused-function)";
+  EXPECT_THAT(TU.build().getDiagnostics(), ElementsAre(Warning));
+}
+
+TEST(DiagnosticTest, ClangTidyWarningOptionsBeforeBeginSourceFile) {
+  auto Build = [](bool Suppress, bool ExtraArgsBefore) {
+    auto TU = TestTU::withCode("int value;");
+    TU.ClangTidyProvider = [=](tidy::ClangTidyOptions &Opts, llvm::StringRef) {
+      Opts.Checks = "-*";
+      if (Suppress) {
+        auto &Args = ExtraArgsBefore ? Opts.ExtraArgsBefore : Opts.ExtraArgs;
+        Args = {"-Wno-incomplete-umbrella"};
+      }
+    };
+    TU.ExtraArgs = {"-fmodules",
+                    "-fmodule-map-file=" + testPath("module.modulemap")};
+    TU.AdditionalFiles["module.modulemap"] =
+        R"(module M { umbrella "missing" })";
+
+    // Don't build a PCH; count warnings from module-map loading directly.
+    MockFS FS;
+    auto Inputs = TU.inputs(FS);
+    IgnoringDiagConsumer Diags;
+    auto CI = buildCompilerInvocation(Inputs, Diags);
+    ASSERT_TRUE(CI);
+    auto AST = ParsedAST::build(testPath(TU.Filename), Inputs, std::move(CI),
+                                {}, nullptr);
+    ASSERT_TRUE(AST);
+    EXPECT_EQ(AST->getPreprocessor().getDiagnostics().getNumWarnings(),
+              Suppress ? 0u : 1u);
+  };
+  Build(false, false);
+  Build(true, false);
+  Build(true, true);
+}
+
+TEST(DiagnosticTest, ClangTidyRelativeInput) {
+  if (!CLANGD_TIDY_CHECKS)
+    GTEST_SKIP() << "Requires clang-tidy checks";
+  MockFS FS;
+  auto TU = TestTU::withCode("int *p = 0;");
+  TU.Filename = "project/src/test.cpp";
+  TU.AdditionalFiles["project/src/.clang-tidy"] =
+      "Checks: '-*,modernize-use-nullptr'";
+  auto Provider = provideClangTidyFiles(FS);
+  TU.ClangTidyProvider = [&](tidy::ClangTidyOptions &Opts,
+                             llvm::StringRef Filename) {
+    EXPECT_EQ(Filename, testPath(TU.Filename));
+    Provider(Opts, Filename);
+  };
+  auto Inputs = TU.inputs(FS);
+  Inputs.CompileCommand.Directory = testPath("project");
+  // Configuration lookup must use the requested file, not the frontend input.
+  for (auto InputFile : {"src/./test.cpp", "../other/test.cpp"}) {
+    SCOPED_TRACE(InputFile);
+    IgnoringDiagConsumer Diags;
+    auto CI = buildCompilerInvocation(Inputs, Diags);
+    ASSERT_TRUE(CI);
+    auto Kind = CI->getFrontendOpts().Inputs.front().getKind();
+    CI->getFrontendOpts().Inputs = {FrontendInputFile(InputFile, Kind)};
+    auto AST = ParsedAST::build(testPath(TU.Filename), Inputs, std::move(CI),
----------------
timon-ul wrote:

Thank you for the explanation, I think I understand now what is happening. That 
being said the "why" is still a bit unclear to me. Basically the name from 
`Inputs` is mapped to the content of the `TU`, but the actual filename could be 
something completly different. I assume it is not supposed to be something 
completly different though, just that there we allow relative paths and we 
additionally need to pass the absolute path to `build()` since it needs it. Is 
this correct?

https://github.com/llvm/llvm-project/pull/231461
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to