ArgumentsAdjusters.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #include "clang/Tooling/ArgumentsAdjusters.h"
  15. #include "clang/Basic/LLVM.h"
  16. #include "llvm/ADT/StringRef.h"
  17. namespace clang {
  18. namespace tooling {
  19. /// Add -fsyntax-only option to the commnand line arguments.
  20. ArgumentsAdjuster getClangSyntaxOnlyAdjuster() {
  21. return [](const CommandLineArguments &Args) {
  22. CommandLineArguments AdjustedArgs;
  23. for (size_t i = 0, e = Args.size(); i != e; ++i) {
  24. StringRef Arg = Args[i];
  25. // FIXME: Remove options that generate output.
  26. if (!Arg.startswith("-fcolor-diagnostics") &&
  27. !Arg.startswith("-fdiagnostics-color"))
  28. AdjustedArgs.push_back(Args[i]);
  29. }
  30. AdjustedArgs.push_back("-fsyntax-only");
  31. return AdjustedArgs;
  32. };
  33. }
  34. ArgumentsAdjuster getClangStripOutputAdjuster() {
  35. return [](const CommandLineArguments &Args) {
  36. CommandLineArguments AdjustedArgs;
  37. for (size_t i = 0, e = Args.size(); i < e; ++i) {
  38. StringRef Arg = Args[i];
  39. if (!Arg.startswith("-o"))
  40. AdjustedArgs.push_back(Args[i]);
  41. if (Arg == "-o") {
  42. // Output is specified as -o foo. Skip the next argument also.
  43. ++i;
  44. }
  45. // Else, the output is specified as -ofoo. Just do nothing.
  46. }
  47. return AdjustedArgs;
  48. };
  49. }
  50. ArgumentsAdjuster getInsertArgumentAdjuster(const CommandLineArguments &Extra,
  51. ArgumentInsertPosition Pos) {
  52. return [Extra, Pos](const CommandLineArguments &Args) {
  53. CommandLineArguments Return(Args);
  54. CommandLineArguments::iterator I;
  55. if (Pos == ArgumentInsertPosition::END) {
  56. I = Return.end();
  57. } else {
  58. I = Return.begin();
  59. ++I; // To leave the program name in place
  60. }
  61. Return.insert(I, Extra.begin(), Extra.end());
  62. return Return;
  63. };
  64. }
  65. ArgumentsAdjuster getInsertArgumentAdjuster(const char *Extra,
  66. ArgumentInsertPosition Pos) {
  67. return getInsertArgumentAdjuster(CommandLineArguments(1, Extra), Pos);
  68. }
  69. ArgumentsAdjuster combineAdjusters(ArgumentsAdjuster First,
  70. ArgumentsAdjuster Second) {
  71. return [First, Second](const CommandLineArguments &Args) {
  72. return Second(First(Args));
  73. };
  74. }
  75. } // end namespace tooling
  76. } // end namespace clang