[PATCH] D70173: [clang][Tooling] Filter flags that generate output in SyntaxOnlyAdjuster

Kadir Cetinkaya via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 13 04:52:49 PST 2019


kadircet created this revision.
kadircet added a reviewer: hokein.
Herald added subscribers: cfe-commits, usaxena95, ilya-biryukov.
Herald added a project: clang.

Flags that generate output could result in failures when creating
syntax only actions. This patch introduces initial logic for filtering out
those. The first such flag is "save-temps", which saves intermediate
files(bitcode, assembly, etc.) into a specified directory.

Fixes https://github.com/clangd/clangd/issues/191


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D70173

Files:
  clang/lib/Tooling/ArgumentsAdjusters.cpp
  clang/unittests/Tooling/ToolingTest.cpp


Index: clang/unittests/Tooling/ToolingTest.cpp
===================================================================
--- clang/unittests/Tooling/ToolingTest.cpp
+++ clang/unittests/Tooling/ToolingTest.cpp
@@ -13,15 +13,18 @@
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Frontend/FrontendAction.h"
 #include "clang/Frontend/FrontendActions.h"
+#include "clang/Tooling/ArgumentsAdjusters.h"
 #include "clang/Tooling/CompilationDatabase.h"
 #include "clang/Tooling/Tooling.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/TargetRegistry.h"
 #include "llvm/Support/TargetSelect.h"
 #include "gtest/gtest.h"
 #include <algorithm>
 #include <string>
+#include <vector>
 
 namespace clang {
 namespace tooling {
@@ -429,6 +432,37 @@
   EXPECT_EQ(SyntaxOnlyCount, 1U);
 }
 
+TEST(ClangToolTest, NoOutputCommands) {
+  FixedCompilationDatabase Compilations("/", {"-save-temps", "-save-temps=cwd",
+                                              "--save-temps",
+                                              "--save-temps=somedir"});
+
+  ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cc"));
+  Tool.mapVirtualFile("/a.cc", "void a() {}");
+
+  std::unique_ptr<FrontendActionFactory> Action(
+      newFrontendActionFactory<SyntaxOnlyAction>());
+
+  const std::vector<llvm::StringRef> OutputCommands = {"-save-temps"};
+  bool Ran = false;
+  ArgumentsAdjuster CheckSyntaxOnlyAdjuster =
+      [&OutputCommands, &Ran](const CommandLineArguments &Args,
+                              StringRef /*unused*/) {
+        for (llvm::StringRef Arg : Args) {
+          for (llvm::StringRef OutputCommand : OutputCommands)
+            EXPECT_FALSE(Arg.contains(OutputCommand));
+        }
+        Ran = true;
+        return Args;
+      };
+
+  Tool.clearArgumentsAdjusters();
+  Tool.appendArgumentsAdjuster(getClangSyntaxOnlyAdjuster());
+  Tool.appendArgumentsAdjuster(CheckSyntaxOnlyAdjuster);
+  Tool.run(Action.get());
+  EXPECT_TRUE(Ran);
+}
+
 TEST(ClangToolTest, BaseVirtualFileSystemUsage) {
   FixedCompilationDatabase Compilations("/", std::vector<std::string>());
   llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
Index: clang/lib/Tooling/ArgumentsAdjusters.cpp
===================================================================
--- clang/lib/Tooling/ArgumentsAdjusters.cpp
+++ clang/lib/Tooling/ArgumentsAdjusters.cpp
@@ -13,8 +13,10 @@
 
 #include "clang/Tooling/ArgumentsAdjusters.h"
 #include "clang/Basic/LLVM.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/StringRef.h"
 #include <cstddef>
+#include <vector>
 
 namespace clang {
 namespace tooling {
@@ -24,9 +26,19 @@
   return [](const CommandLineArguments &Args, StringRef /*unused*/) {
     CommandLineArguments AdjustedArgs;
     bool HasSyntaxOnly = false;
+    const std::vector<llvm::StringRef> OutputCommands = {
+        // FIXME: Add other options that generate output.
+        "-save-temps",
+        "--save-temps",
+    };
     for (size_t i = 0, e = Args.size(); i < e; ++i) {
       StringRef Arg = Args[i];
-      // FIXME: Remove options that generate output.
+      // Skip output commands.
+      if (llvm::any_of(OutputCommands, [&Arg](llvm::StringRef OutputCommand) {
+            return Arg.startswith(OutputCommand);
+          }))
+        continue;
+
       if (!Arg.startswith("-fcolor-diagnostics") &&
           !Arg.startswith("-fdiagnostics-color"))
         AdjustedArgs.push_back(Args[i]);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D70173.229062.patch
Type: text/x-patch
Size: 3523 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20191113/a8c9c692/attachment.bin>


More information about the cfe-commits mailing list