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