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 #include <cstddef> 19 20 namespace clang { 21 namespace tooling { 22 23 /// Add -fsyntax-only option to the command line arguments. 24 ArgumentsAdjuster getClangSyntaxOnlyAdjuster() { 25 return [](const CommandLineArguments &Args, StringRef /*unused*/) { 26 CommandLineArguments AdjustedArgs; 27 for (size_t i = 0, e = Args.size(); i < e; ++i) { 28 StringRef Arg = Args[i]; 29 // FIXME: Remove options that generate output. 30 if (!Arg.startswith("-fcolor-diagnostics") && 31 !Arg.startswith("-fdiagnostics-color")) 32 AdjustedArgs.push_back(Args[i]); 33 } 34 AdjustedArgs.push_back("-fsyntax-only"); 35 return AdjustedArgs; 36 }; 37 } 38 39 ArgumentsAdjuster getClangStripOutputAdjuster() { 40 return [](const CommandLineArguments &Args, StringRef /*unused*/) { 41 CommandLineArguments AdjustedArgs; 42 for (size_t i = 0, e = Args.size(); i < e; ++i) { 43 StringRef Arg = Args[i]; 44 if (!Arg.startswith("-o")) 45 AdjustedArgs.push_back(Args[i]); 46 47 if (Arg == "-o") { 48 // Output is specified as -o foo. Skip the next argument too. 49 ++i; 50 } 51 // Else, the output is specified as -ofoo. Just do nothing. 52 } 53 return AdjustedArgs; 54 }; 55 } 56 57 ArgumentsAdjuster getClangStripDependencyFileAdjuster() { 58 return [](const CommandLineArguments &Args, StringRef /*unused*/) { 59 CommandLineArguments AdjustedArgs; 60 for (size_t i = 0, e = Args.size(); i < e; ++i) { 61 StringRef Arg = Args[i]; 62 // All dependency-file options begin with -M. These include -MM, 63 // -MF, -MG, -MP, -MT, -MQ, -MD, and -MMD. 64 if (!Arg.startswith("-M")) { 65 AdjustedArgs.push_back(Args[i]); 66 continue; 67 } 68 69 if (Arg == "-MF" || Arg == "-MT" || Arg == "-MQ") 70 // These flags take an argument: -MX foo. Skip the next argument also. 71 ++i; 72 } 73 return AdjustedArgs; 74 }; 75 } 76 77 ArgumentsAdjuster getInsertArgumentAdjuster(const CommandLineArguments &Extra, 78 ArgumentInsertPosition Pos) { 79 return [Extra, Pos](const CommandLineArguments &Args, StringRef /*unused*/) { 80 CommandLineArguments Return(Args); 81 82 CommandLineArguments::iterator I; 83 if (Pos == ArgumentInsertPosition::END) { 84 I = Return.end(); 85 } else { 86 I = Return.begin(); 87 ++I; // To leave the program name in place 88 } 89 90 Return.insert(I, Extra.begin(), Extra.end()); 91 return Return; 92 }; 93 } 94 95 ArgumentsAdjuster getInsertArgumentAdjuster(const char *Extra, 96 ArgumentInsertPosition Pos) { 97 return getInsertArgumentAdjuster(CommandLineArguments(1, Extra), Pos); 98 } 99 100 ArgumentsAdjuster combineAdjusters(ArgumentsAdjuster First, 101 ArgumentsAdjuster Second) { 102 if (!First) 103 return Second; 104 if (!Second) 105 return First; 106 return [First, Second](const CommandLineArguments &Args, StringRef File) { 107 return Second(First(Args, File), File); 108 }; 109 } 110 111 } // end namespace tooling 112 } // end namespace clang 113