Coverage Report

Created: 2020-02-25 14:32

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Tooling/ArgumentsAdjusters.cpp
Line
Count
Source
1
//===- ArgumentsAdjusters.cpp - Command line arguments adjuster -----------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file contains definitions of classes which implement ArgumentsAdjuster
10
// interface.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/Tooling/ArgumentsAdjusters.h"
15
#include "clang/Basic/LLVM.h"
16
#include "llvm/ADT/STLExtras.h"
17
#include "llvm/ADT/StringRef.h"
18
#include <cstddef>
19
#include <vector>
20
21
namespace clang {
22
namespace tooling {
23
24
/// Add -fsyntax-only option and drop options that triggers output generation.
25
432
ArgumentsAdjuster getClangSyntaxOnlyAdjuster() {
26
432
  return [](const CommandLineArguments &Args, StringRef /*unused*/) {
27
379
    CommandLineArguments AdjustedArgs;
28
379
    bool HasSyntaxOnly = false;
29
379
    constexpr llvm::StringRef OutputCommands[] = {
30
379
        // FIXME: Add other options that generate output.
31
379
        "-save-temps",
32
379
        "--save-temps",
33
379
    };
34
1.28k
    for (size_t i = 0, e = Args.size(); i < e; 
++i908
) {
35
908
      StringRef Arg = Args[i];
36
908
      // Skip output commands.
37
1.81k
      if (
llvm::any_of(OutputCommands, [&Arg](llvm::StringRef OutputCommand) 908
{
38
1.81k
            return Arg.startswith(OutputCommand);
39
1.81k
          }))
40
4
        continue;
41
904
42
904
      if (!Arg.startswith("-fcolor-diagnostics") &&
43
906
          !Arg.startswith("-fdiagnostics-color"))
44
906
        AdjustedArgs.push_back(Args[i]);
45
904
      if (Arg == "-fsyntax-only")
46
105
        HasSyntaxOnly = true;
47
904
    }
48
379
    if (!HasSyntaxOnly)
49
275
      AdjustedArgs.push_back("-fsyntax-only");
50
379
    return AdjustedArgs;
51
379
  };
52
432
}
53
54
467
ArgumentsAdjuster getClangStripOutputAdjuster() {
55
467
  return [](const CommandLineArguments &Args, StringRef /*unused*/) {
56
415
    CommandLineArguments AdjustedArgs;
57
1.46k
    for (size_t i = 0, e = Args.size(); i < e; 
++i1.05k
) {
58
1.05k
      StringRef Arg = Args[i];
59
1.05k
      if (!Arg.startswith("-o"))
60
1.04k
        AdjustedArgs.push_back(Args[i]);
61
1.05k
62
1.05k
      if (Arg == "-o") {
63
1
        // Output is specified as -o foo. Skip the next argument too.
64
1
        ++i;
65
1
      }
66
1.05k
      // Else, the output is specified as -ofoo. Just do nothing.
67
1.05k
    }
68
415
    return AdjustedArgs;
69
415
  };
70
467
}
71
72
27
ArgumentsAdjuster getClangStripSerializeDiagnosticAdjuster() {
73
65
  return [](const CommandLineArguments &Args, StringRef /*unused*/) {
74
65
    CommandLineArguments AdjustedArgs;
75
1.23k
    for (size_t i = 0, e = Args.size(); i < e; 
++i1.16k
) {
76
1.16k
      StringRef Arg = Args[i];
77
1.16k
      if (Arg == "--serialize-diagnostics") {
78
1
        // Skip the diagnostic output argument.
79
1
        ++i;
80
1
        continue;
81
1
      }
82
1.16k
      AdjustedArgs.push_back(Args[i]);
83
1.16k
    }
84
65
    return AdjustedArgs;
85
65
  };
86
27
}
87
88
9.05k
ArgumentsAdjuster getClangStripDependencyFileAdjuster() {
89
9.05k
  return [](const CommandLineArguments &Args, StringRef /*unused*/) {
90
9.00k
    CommandLineArguments AdjustedArgs;
91
36.3k
    for (size_t i = 0, e = Args.size(); i < e; 
++i27.2k
) {
92
27.2k
      StringRef Arg = Args[i];
93
27.2k
      // All dependency-file options begin with -M. These include -MM,
94
27.2k
      // -MF, -MG, -MP, -MT, -MQ, -MD, and -MMD.
95
27.2k
      if (!Arg.startswith("-M")) {
96
27.2k
        AdjustedArgs.push_back(Args[i]);
97
27.2k
        continue;
98
27.2k
      }
99
5
100
5
      if (Arg == "-MF" || 
Arg == "-MT"4
||
Arg == "-MQ"3
)
101
2
        // These flags take an argument: -MX foo. Skip the next argument also.
102
2
        ++i;
103
5
    }
104
9.00k
    return AdjustedArgs;
105
9.00k
  };
106
9.05k
}
107
108
ArgumentsAdjuster getInsertArgumentAdjuster(const CommandLineArguments &Extra,
109
308
                                            ArgumentInsertPosition Pos) {
110
435
  return [Extra, Pos](const CommandLineArguments &Args, StringRef /*unused*/) {
111
435
    CommandLineArguments Return(Args);
112
435
113
435
    CommandLineArguments::iterator I;
114
435
    if (Pos == ArgumentInsertPosition::END) {
115
198
      I = Return.end();
116
237
    } else {
117
237
      I = Return.begin();
118
237
      ++I; // To leave the program name in place
119
237
    }
120
435
121
435
    Return.insert(I, Extra.begin(), Extra.end());
122
435
    return Return;
123
435
  };
124
308
}
125
126
ArgumentsAdjuster getInsertArgumentAdjuster(const char *Extra,
127
38
                                            ArgumentInsertPosition Pos) {
128
38
  return getInsertArgumentAdjuster(CommandLineArguments(1, Extra), Pos);
129
38
}
130
131
ArgumentsAdjuster combineAdjusters(ArgumentsAdjuster First,
132
1.63k
                                   ArgumentsAdjuster Second) {
133
1.63k
  if (!First)
134
368
    return Second;
135
1.26k
  if (!Second)
136
102
    return First;
137
1.16k
  return [First, Second](const CommandLineArguments &Args, StringRef File) {
138
1.12k
    return Second(First(Args, File), File);
139
1.12k
  };
140
1.16k
}
141
142
1
ArgumentsAdjuster getStripPluginsAdjuster() {
143
1
  return [](const CommandLineArguments &Args, StringRef /*unused*/) {
144
1
    CommandLineArguments AdjustedArgs;
145
4
    for (size_t I = 0, E = Args.size(); I != E; 
I++3
) {
146
3
      // According to https://clang.llvm.org/docs/ClangPlugins.html
147
3
      // plugin arguments are in the form:
148
3
      // -Xclang {-load, -plugin, -plugin-arg-<plugin-name>, -add-plugin}
149
3
      // -Xclang <arbitrary-argument>
150
3
      if (I + 4 < E && 
Args[I] == "-Xclang"2
&&
151
3
          
(1
Args[I + 1] == "-load"1
||
Args[I + 1] == "-plugin"1
||
152
1
           llvm::StringRef(Args[I + 1]).startswith("-plugin-arg-") ||
153
1
           Args[I + 1] == "-add-plugin") &&
154
3
          
Args[I + 2] == "-Xclang"1
) {
155
1
        I += 3;
156
1
        continue;
157
1
      }
158
2
      AdjustedArgs.push_back(Args[I]);
159
2
    }
160
1
    return AdjustedArgs;
161
1
  };
162
1
}
163
164
} // end namespace tooling
165
} // end namespace clang