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 17 namespace clang { 18 namespace tooling { 19 20 /// Add -fsyntax-only option to the commnand line arguments. 21 ArgumentsAdjuster getClangSyntaxOnlyAdjuster() { 22 return [](const CommandLineArguments &Args, StringRef /*unused*/) { 23 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]); 30 } 31 AdjustedArgs.push_back("-fsyntax-only"); 32 return AdjustedArgs; 33 }; 34 } 35 36 ArgumentsAdjuster getClangStripOutputAdjuster() { 37 return [](const CommandLineArguments &Args, StringRef /*unused*/) { 38 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]); 43 44 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 } 53 54 ArgumentsAdjuster getInsertArgumentAdjuster(const CommandLineArguments &Extra, 55 ArgumentInsertPosition Pos) { 56 return [Extra, Pos](const CommandLineArguments &Args, StringRef /*unused*/) { 57 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 72 ArgumentsAdjuster getInsertArgumentAdjuster(const char *Extra, 73 ArgumentInsertPosition Pos) { 74 return getInsertArgumentAdjuster(CommandLineArguments(1, Extra), Pos); 75 } 76 77 ArgumentsAdjuster combineAdjusters(ArgumentsAdjuster First, 78 ArgumentsAdjuster Second) { 79 return [First, Second](const CommandLineArguments &Args, StringRef File) { 80 return Second(First(Args, File), File); 81 }; 82 } 83 84 } // end namespace tooling 85 } // end namespace clang 86 87