blob: 5a67db0e176f5c9cf5c50d3553d1e7e30ddcf0fd [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"
Alexander Kornienkoafd57012013-06-05 11:33:11 +000016#include "clang/Basic/LLVM.h"
17#include "llvm/ADT/StringRef.h"
Simon Atanasyan32df72d2012-05-09 16:18:30 +000018
19namespace clang {
20namespace tooling {
21
Simon Atanasyan32df72d2012-05-09 16:18:30 +000022/// Add -fsyntax-only option to the commnand line arguments.
Alexander Kornienko74e1c462014-12-03 17:53:02 +000023ArgumentsAdjuster 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]);
Pavel Labathdee20c12013-06-06 11:52:19 +000032 }
Alexander Kornienko74e1c462014-12-03 17:53:02 +000033 AdjustedArgs.push_back("-fsyntax-only");
34 return AdjustedArgs;
35 };
Pavel Labathdee20c12013-06-06 11:52:19 +000036}
37
Alexander Kornienko74e1c462014-12-03 17:53:02 +000038ArgumentsAdjuster 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]);
Alexander Kornienko61686092014-11-04 08:51:24 +000045
Alexander Kornienko74e1c462014-12-03 17:53:02 +000046 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}
Alexander Kornienko61686092014-11-04 08:51:24 +000055
Alexander Kornienko74e1c462014-12-03 17:53:02 +000056ArgumentsAdjuster 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
74ArgumentsAdjuster getInsertArgumentAdjuster(const char *Extra,
75 ArgumentInsertPosition Pos) {
76 return getInsertArgumentAdjuster(CommandLineArguments(1, Extra), Pos);
77}
78
79ArgumentsAdjuster combineAdjusters(ArgumentsAdjuster First,
80 ArgumentsAdjuster Second) {
81 return std::bind(Second, std::bind(First, std::placeholders::_1));
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