1 //===--- ArgumentsAdjusters.cpp - Command line arguments adjuster ---------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains definitions of classes which implement ArgumentsAdjuster
11 // interface.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "clang/Tooling/ArgumentsAdjusters.h"
16 #include "clang/Basic/LLVM.h"
17 #include "llvm/ADT/StringRef.h"
18
19 namespace clang {
20 namespace tooling {
21
22 /// Add -fsyntax-only option to the commnand line arguments.
getClangSyntaxOnlyAdjuster()23 ArgumentsAdjuster getClangSyntaxOnlyAdjuster() {
24 return [](const CommandLineArguments &Args) {
25 CommandLineArguments AdjustedArgs;
26 for (size_t i = 0, e = Args.size(); i != e; ++i) {
27 StringRef Arg = Args[i];
28 // FIXME: Remove options that generate output.
29 if (!Arg.startswith("-fcolor-diagnostics") &&
30 !Arg.startswith("-fdiagnostics-color"))
31 AdjustedArgs.push_back(Args[i]);
32 }
33 AdjustedArgs.push_back("-fsyntax-only");
34 return AdjustedArgs;
35 };
36 }
37
getClangStripOutputAdjuster()38 ArgumentsAdjuster getClangStripOutputAdjuster() {
39 return [](const CommandLineArguments &Args) {
40 CommandLineArguments AdjustedArgs;
41 for (size_t i = 0, e = Args.size(); i < e; ++i) {
42 StringRef Arg = Args[i];
43 if (!Arg.startswith("-o"))
44 AdjustedArgs.push_back(Args[i]);
45
46 if (Arg == "-o") {
47 // Output is specified as -o foo. Skip the next argument also.
48 ++i;
49 }
50 // Else, the output is specified as -ofoo. Just do nothing.
51 }
52 return AdjustedArgs;
53 };
54 }
55
getInsertArgumentAdjuster(const CommandLineArguments & Extra,ArgumentInsertPosition Pos)56 ArgumentsAdjuster getInsertArgumentAdjuster(const CommandLineArguments &Extra,
57 ArgumentInsertPosition Pos) {
58 return [Extra, Pos](const CommandLineArguments &Args) {
59 CommandLineArguments Return(Args);
60
61 CommandLineArguments::iterator I;
62 if (Pos == ArgumentInsertPosition::END) {
63 I = Return.end();
64 } else {
65 I = Return.begin();
66 ++I; // To leave the program name in place
67 }
68
69 Return.insert(I, Extra.begin(), Extra.end());
70 return Return;
71 };
72 }
73
getInsertArgumentAdjuster(const char * Extra,ArgumentInsertPosition Pos)74 ArgumentsAdjuster getInsertArgumentAdjuster(const char *Extra,
75 ArgumentInsertPosition Pos) {
76 return getInsertArgumentAdjuster(CommandLineArguments(1, Extra), Pos);
77 }
78
combineAdjusters(ArgumentsAdjuster First,ArgumentsAdjuster Second)79 ArgumentsAdjuster combineAdjusters(ArgumentsAdjuster First,
80 ArgumentsAdjuster Second) {
81 return [First, Second](const CommandLineArguments &Args) {
82 return Second(First(Args));
83 };
84 }
85
86 } // end namespace tooling
87 } // end namespace clang
88
89