blob: 48b925c698a7c57838229ab16e658a33182b8bf6 [file] [log] [blame]
Simon Atanasyan32df72d2012-05-09 16:18:30 +00001//===--- 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
17namespace clang {
18namespace tooling {
19
Joerg Sonnenberger92d91562016-11-25 20:15:57 +000020/// Add -fsyntax-only option to the command line arguments.
Alexander Kornienko74e1c462014-12-03 17:53:02 +000021ArgumentsAdjuster getClangSyntaxOnlyAdjuster() {
Alexander Kornienko857b10f2015-11-05 02:19:53 +000022 return [](const CommandLineArguments &Args, StringRef /*unused*/) {
Alexander Kornienko74e1c462014-12-03 17:53:02 +000023 CommandLineArguments AdjustedArgs;
24 for (size_t i = 0, e = Args.size(); i != e; ++i) {
25 StringRef Arg = Args[i];
26 // FIXME: Remove options that generate output.
27 if (!Arg.startswith("-fcolor-diagnostics") &&
28 !Arg.startswith("-fdiagnostics-color"))
29 AdjustedArgs.push_back(Args[i]);
Pavel Labathdee20c12013-06-06 11:52:19 +000030 }
Alexander Kornienko74e1c462014-12-03 17:53:02 +000031 AdjustedArgs.push_back("-fsyntax-only");
32 return AdjustedArgs;
33 };
Pavel Labathdee20c12013-06-06 11:52:19 +000034}
35
Alexander Kornienko74e1c462014-12-03 17:53:02 +000036ArgumentsAdjuster getClangStripOutputAdjuster() {
Alexander Kornienko857b10f2015-11-05 02:19:53 +000037 return [](const CommandLineArguments &Args, StringRef /*unused*/) {
Alexander Kornienko74e1c462014-12-03 17:53:02 +000038 CommandLineArguments AdjustedArgs;
39 for (size_t i = 0, e = Args.size(); i < e; ++i) {
40 StringRef Arg = Args[i];
41 if (!Arg.startswith("-o"))
42 AdjustedArgs.push_back(Args[i]);
Alexander Kornienko61686092014-11-04 08:51:24 +000043
Alexander Kornienko74e1c462014-12-03 17:53:02 +000044 if (Arg == "-o") {
45 // Output is specified as -o foo. Skip the next argument also.
46 ++i;
47 }
48 // Else, the output is specified as -ofoo. Just do nothing.
49 }
50 return AdjustedArgs;
51 };
52}
Alexander Kornienko61686092014-11-04 08:51:24 +000053
Alexander Kornienko74e1c462014-12-03 17:53:02 +000054ArgumentsAdjuster getInsertArgumentAdjuster(const CommandLineArguments &Extra,
55 ArgumentInsertPosition Pos) {
Alexander Kornienko857b10f2015-11-05 02:19:53 +000056 return [Extra, Pos](const CommandLineArguments &Args, StringRef /*unused*/) {
Alexander Kornienko74e1c462014-12-03 17:53:02 +000057 CommandLineArguments Return(Args);
58
59 CommandLineArguments::iterator I;
60 if (Pos == ArgumentInsertPosition::END) {
61 I = Return.end();
62 } else {
63 I = Return.begin();
64 ++I; // To leave the program name in place
65 }
66
67 Return.insert(I, Extra.begin(), Extra.end());
68 return Return;
69 };
70}
71
72ArgumentsAdjuster getInsertArgumentAdjuster(const char *Extra,
73 ArgumentInsertPosition Pos) {
74 return getInsertArgumentAdjuster(CommandLineArguments(1, Extra), Pos);
75}
76
77ArgumentsAdjuster combineAdjusters(ArgumentsAdjuster First,
78 ArgumentsAdjuster Second) {
Alexander Kornienko857b10f2015-11-05 02:19:53 +000079 return [First, Second](const CommandLineArguments &Args, StringRef File) {
80 return Second(First(Args, File), File);
Alexander Kornienkod1f1fa22015-01-16 15:57:15 +000081 };
Alexander Kornienko61686092014-11-04 08:51:24 +000082}
83
Simon Atanasyan32df72d2012-05-09 16:18:30 +000084} // end namespace tooling
85} // end namespace clang
86