Skip to content

Commit 2b311bb

Browse files
Allow CompilerInvocations to generate .d files.
Summary: Most clang tools should ignore the -M family of options because one wouldn't want them to generate a new dependency (.d) file. However, some tools may want this dependency file. This patch creates a mechanism for them to do this. This implementation just plumbs a boolean down several layers of calls. Each of the modified calls has several call sites, and so a single member variable or new API entry point won't work. An alternative would be to write a function to filter the -M family of arguments out of CC1Args, and have each caller call that function by hand before calling newInvocation, Invocation::run, or buildAstFromCodeWithArgs. This is a more complicated and error-prone solution. Why burden all the callers to remember to use this function? But I could rewrite this patch to use that method if that is deemed more appropriate. Reviewers: klimek Reviewed By: klimek Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D34304 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@307315 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 70922a4 commit 2b311bb

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

include/clang/Tooling/ArgumentsAdjusters.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ ArgumentsAdjuster getClangSyntaxOnlyAdjuster();
4444
/// arguments.
4545
ArgumentsAdjuster getClangStripOutputAdjuster();
4646

47+
/// \brief Gets an argument adjuster which removes dependency-file
48+
/// related command line arguments.
49+
ArgumentsAdjuster getClangStripDependencyFileAdjuster();
50+
4751
enum class ArgumentInsertPosition { BEGIN, END };
4852

4953
/// \brief Gets an argument adjuster which inserts \p Extra arguments in the

include/clang/Tooling/Tooling.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,15 @@ buildASTFromCode(const Twine &Code, const Twine &FileName = "input.cc",
202202
/// \param PCHContainerOps The PCHContainerOperations for loading and creating
203203
/// clang modules.
204204
///
205+
/// \param Adjuster A function to filter the command line arguments as specified.
206+
///
205207
/// \return The resulting AST or null if an error occurred.
206208
std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
207209
const Twine &Code, const std::vector<std::string> &Args,
208210
const Twine &FileName = "input.cc", const Twine &ToolName = "clang-tool",
209211
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
210-
std::make_shared<PCHContainerOperations>());
212+
std::make_shared<PCHContainerOperations>(),
213+
ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster());
211214

212215
/// \brief Utility to run a FrontendAction in a single clang invocation.
213216
class ToolInvocation {

lib/Tooling/ArgumentsAdjusters.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ ArgumentsAdjuster getClangStripOutputAdjuster() {
4242
AdjustedArgs.push_back(Args[i]);
4343

4444
if (Arg == "-o") {
45-
// Output is specified as -o foo. Skip the next argument also.
45+
// Output is specified as -o foo. Skip the next argument too.
4646
++i;
4747
}
4848
// Else, the output is specified as -ofoo. Just do nothing.
@@ -51,6 +51,26 @@ ArgumentsAdjuster getClangStripOutputAdjuster() {
5151
};
5252
}
5353

54+
ArgumentsAdjuster getClangStripDependencyFileAdjuster() {
55+
return [](const CommandLineArguments &Args, StringRef /*unused*/) {
56+
CommandLineArguments AdjustedArgs;
57+
for (size_t i = 0, e = Args.size(); i < e; ++i) {
58+
StringRef Arg = Args[i];
59+
// All dependency-file options begin with -M. These include -MM,
60+
// -MF, -MG, -MP, -MT, -MQ, -MD, and -MMD.
61+
if (!Arg.startswith("-M"))
62+
AdjustedArgs.push_back(Args[i]);
63+
64+
if ((Arg == "-MF") || (Arg == "-MT") || (Arg == "-MQ") ||
65+
(Arg == "-MD") || (Arg == "-MMD")) {
66+
// Output is specified as -MX foo. Skip the next argument also.
67+
++i;
68+
}
69+
}
70+
return AdjustedArgs;
71+
};
72+
}
73+
5474
ArgumentsAdjuster getInsertArgumentAdjuster(const CommandLineArguments &Extra,
5575
ArgumentInsertPosition Pos) {
5676
return [Extra, Pos](const CommandLineArguments &Args, StringRef /*unused*/) {
@@ -83,4 +103,3 @@ ArgumentsAdjuster combineAdjusters(ArgumentsAdjuster First,
83103

84104
} // end namespace tooling
85105
} // end namespace clang
86-

lib/Tooling/Tooling.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ clang::CompilerInvocation *newInvocation(
100100
*Diagnostics);
101101
Invocation->getFrontendOpts().DisableFree = false;
102102
Invocation->getCodeGenOpts().DisableFree = false;
103-
Invocation->getDependencyOutputOpts() = DependencyOutputOptions();
104103
return Invocation;
105104
}
106105

@@ -510,7 +509,8 @@ buildASTFromCode(const Twine &Code, const Twine &FileName,
510509
std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
511510
const Twine &Code, const std::vector<std::string> &Args,
512511
const Twine &FileName, const Twine &ToolName,
513-
std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
512+
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
513+
ArgumentsAdjuster Adjuster) {
514514
SmallString<16> FileNameStorage;
515515
StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage);
516516

@@ -523,8 +523,10 @@ std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
523523
OverlayFileSystem->pushOverlay(InMemoryFileSystem);
524524
llvm::IntrusiveRefCntPtr<FileManager> Files(
525525
new FileManager(FileSystemOptions(), OverlayFileSystem));
526-
ToolInvocation Invocation(getSyntaxOnlyToolArgs(ToolName, Args, FileNameRef),
527-
&Action, Files.get(), std::move(PCHContainerOps));
526+
527+
ToolInvocation Invocation(
528+
getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileNameRef), FileNameRef),
529+
&Action, Files.get(), std::move(PCHContainerOps));
528530

529531
SmallString<1024> CodeStorage;
530532
InMemoryFileSystem->addFile(FileNameRef, 0,

0 commit comments

Comments
 (0)