Skip to content

Commit 16bdcc8

Browse files
committed
[clang][Tooling] Filter flags that generate output in SyntaxOnlyAdjuster
Summary: 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 clangd/clangd#191 Reviewers: hokein Subscribers: ilya-biryukov, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D70173
1 parent 33e882d commit 16bdcc8

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

clang/lib/Tooling/ArgumentsAdjusters.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,32 @@
1313

1414
#include "clang/Tooling/ArgumentsAdjusters.h"
1515
#include "clang/Basic/LLVM.h"
16+
#include "llvm/ADT/STLExtras.h"
1617
#include "llvm/ADT/StringRef.h"
1718
#include <cstddef>
19+
#include <vector>
1820

1921
namespace clang {
2022
namespace tooling {
2123

22-
/// Add -fsyntax-only option to the command line arguments.
24+
/// Add -fsyntax-only option and drop options that triggers output generation.
2325
ArgumentsAdjuster getClangSyntaxOnlyAdjuster() {
2426
return [](const CommandLineArguments &Args, StringRef /*unused*/) {
2527
CommandLineArguments AdjustedArgs;
2628
bool HasSyntaxOnly = false;
29+
const std::vector<llvm::StringRef> OutputCommands = {
30+
// FIXME: Add other options that generate output.
31+
"-save-temps",
32+
"--save-temps",
33+
};
2734
for (size_t i = 0, e = Args.size(); i < e; ++i) {
2835
StringRef Arg = Args[i];
29-
// FIXME: Remove options that generate output.
36+
// Skip output commands.
37+
if (llvm::any_of(OutputCommands, [&Arg](llvm::StringRef OutputCommand) {
38+
return Arg.startswith(OutputCommand);
39+
}))
40+
continue;
41+
3042
if (!Arg.startswith("-fcolor-diagnostics") &&
3143
!Arg.startswith("-fdiagnostics-color"))
3244
AdjustedArgs.push_back(Args[i]);

clang/unittests/Tooling/ToolingTest.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@
1313
#include "clang/Frontend/CompilerInstance.h"
1414
#include "clang/Frontend/FrontendAction.h"
1515
#include "clang/Frontend/FrontendActions.h"
16+
#include "clang/Tooling/ArgumentsAdjusters.h"
1617
#include "clang/Tooling/CompilationDatabase.h"
1718
#include "clang/Tooling/Tooling.h"
1819
#include "llvm/ADT/STLExtras.h"
20+
#include "llvm/ADT/StringRef.h"
1921
#include "llvm/Support/Path.h"
2022
#include "llvm/Support/TargetRegistry.h"
2123
#include "llvm/Support/TargetSelect.h"
2224
#include "gtest/gtest.h"
2325
#include <algorithm>
2426
#include <string>
27+
#include <vector>
2528

2629
namespace clang {
2730
namespace tooling {
@@ -429,6 +432,37 @@ TEST(ClangToolTest, NoDoubleSyntaxOnly) {
429432
EXPECT_EQ(SyntaxOnlyCount, 1U);
430433
}
431434

435+
TEST(ClangToolTest, NoOutputCommands) {
436+
FixedCompilationDatabase Compilations("/", {"-save-temps", "-save-temps=cwd",
437+
"--save-temps",
438+
"--save-temps=somedir"});
439+
440+
ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cc"));
441+
Tool.mapVirtualFile("/a.cc", "void a() {}");
442+
443+
std::unique_ptr<FrontendActionFactory> Action(
444+
newFrontendActionFactory<SyntaxOnlyAction>());
445+
446+
const std::vector<llvm::StringRef> OutputCommands = {"-save-temps"};
447+
bool Ran = false;
448+
ArgumentsAdjuster CheckSyntaxOnlyAdjuster =
449+
[&OutputCommands, &Ran](const CommandLineArguments &Args,
450+
StringRef /*unused*/) {
451+
for (llvm::StringRef Arg : Args) {
452+
for (llvm::StringRef OutputCommand : OutputCommands)
453+
EXPECT_FALSE(Arg.contains(OutputCommand));
454+
}
455+
Ran = true;
456+
return Args;
457+
};
458+
459+
Tool.clearArgumentsAdjusters();
460+
Tool.appendArgumentsAdjuster(getClangSyntaxOnlyAdjuster());
461+
Tool.appendArgumentsAdjuster(CheckSyntaxOnlyAdjuster);
462+
Tool.run(Action.get());
463+
EXPECT_TRUE(Ran);
464+
}
465+
432466
TEST(ClangToolTest, BaseVirtualFileSystemUsage) {
433467
FixedCompilationDatabase Compilations("/", std::vector<std::string>());
434468
llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(

0 commit comments

Comments
 (0)