30
30
#include " llvm/ADT/ArrayRef.h"
31
31
#include " llvm/ADT/SmallString.h"
32
32
#include " llvm/ADT/SmallVector.h"
33
+ #include " llvm/ADT/StringSet.h"
33
34
#include " llvm/Option/ArgList.h"
34
35
#include " llvm/Option/OptTable.h"
35
36
#include " llvm/Option/Option.h"
45
46
#include " llvm/Support/PrettyStackTrace.h"
46
47
#include " llvm/Support/Process.h"
47
48
#include " llvm/Support/Program.h"
48
- #include " llvm/Support/Regex.h"
49
49
#include " llvm/Support/Signals.h"
50
50
#include " llvm/Support/StringSaver.h"
51
51
#include " llvm/Support/TargetSelect.h"
@@ -82,136 +82,8 @@ std::string GetExecutablePath(const char *Argv0, bool CanonicalPrefixes) {
82
82
return llvm::sys::fs::getMainExecutable (Argv0, P);
83
83
}
84
84
85
- static const char *GetStableCStr (std::set<std::string> &SavedStrings,
86
- StringRef S) {
87
- return SavedStrings.insert (std::string (S)).first ->c_str ();
88
- }
89
-
90
- // / ApplyQAOverride - Apply a list of edits to the input argument lists.
91
- // /
92
- // / The input string is a space separate list of edits to perform,
93
- // / they are applied in order to the input argument lists. Edits
94
- // / should be one of the following forms:
95
- // /
96
- // / '#': Silence information about the changes to the command line arguments.
97
- // /
98
- // / '^': Add FOO as a new argument at the beginning of the command line.
99
- // /
100
- // / '+': Add FOO as a new argument at the end of the command line.
101
- // /
102
- // / 's/XXX/YYY/': Substitute the regular expression XXX with YYY in the command
103
- // / line.
104
- // /
105
- // / 'xOPTION': Removes all instances of the literal argument OPTION.
106
- // /
107
- // / 'XOPTION': Removes all instances of the literal argument OPTION,
108
- // / and the following argument.
109
- // /
110
- // / 'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox'
111
- // / at the end of the command line.
112
- // /
113
- // / \param OS - The stream to write edit information to.
114
- // / \param Args - The vector of command line arguments.
115
- // / \param Edit - The override command to perform.
116
- // / \param SavedStrings - Set to use for storing string representations.
117
- static void ApplyOneQAOverride (raw_ostream &OS,
118
- SmallVectorImpl<const char *> &Args,
119
- StringRef Edit,
120
- std::set<std::string> &SavedStrings) {
121
- // This does not need to be efficient.
122
-
123
- if (Edit[0 ] == ' ^' ) {
124
- const char *Str =
125
- GetStableCStr (SavedStrings, Edit.substr (1 ));
126
- OS << " ### Adding argument " << Str << " at beginning\n " ;
127
- Args.insert (Args.begin () + 1 , Str);
128
- } else if (Edit[0 ] == ' +' ) {
129
- const char *Str =
130
- GetStableCStr (SavedStrings, Edit.substr (1 ));
131
- OS << " ### Adding argument " << Str << " at end\n " ;
132
- Args.push_back (Str);
133
- } else if (Edit[0 ] == ' s' && Edit[1 ] == ' /' && Edit.endswith (" /" ) &&
134
- Edit.slice (2 , Edit.size () - 1 ).contains (' /' )) {
135
- StringRef MatchPattern = Edit.substr (2 ).split (' /' ).first ;
136
- StringRef ReplPattern = Edit.substr (2 ).split (' /' ).second ;
137
- ReplPattern = ReplPattern.slice (0 , ReplPattern.size ()-1 );
138
-
139
- for (unsigned i = 1 , e = Args.size (); i != e; ++i) {
140
- // Ignore end-of-line response file markers
141
- if (Args[i] == nullptr )
142
- continue ;
143
- std::string Repl = llvm::Regex (MatchPattern).sub (ReplPattern, Args[i]);
144
-
145
- if (Repl != Args[i]) {
146
- OS << " ### Replacing '" << Args[i] << " ' with '" << Repl << " '\n " ;
147
- Args[i] = GetStableCStr (SavedStrings, Repl);
148
- }
149
- }
150
- } else if (Edit[0 ] == ' x' || Edit[0 ] == ' X' ) {
151
- auto Option = Edit.substr (1 );
152
- for (unsigned i = 1 ; i < Args.size ();) {
153
- if (Option == Args[i]) {
154
- OS << " ### Deleting argument " << Args[i] << ' \n ' ;
155
- Args.erase (Args.begin () + i);
156
- if (Edit[0 ] == ' X' ) {
157
- if (i < Args.size ()) {
158
- OS << " ### Deleting argument " << Args[i] << ' \n ' ;
159
- Args.erase (Args.begin () + i);
160
- } else
161
- OS << " ### Invalid X edit, end of command line!\n " ;
162
- }
163
- } else
164
- ++i;
165
- }
166
- } else if (Edit[0 ] == ' O' ) {
167
- for (unsigned i = 1 ; i < Args.size ();) {
168
- const char *A = Args[i];
169
- // Ignore end-of-line response file markers
170
- if (A == nullptr )
171
- continue ;
172
- if (A[0 ] == ' -' && A[1 ] == ' O' &&
173
- (A[2 ] == ' \0 ' ||
174
- (A[3 ] == ' \0 ' && (A[2 ] == ' s' || A[2 ] == ' z' ||
175
- (' 0' <= A[2 ] && A[2 ] <= ' 9' ))))) {
176
- OS << " ### Deleting argument " << Args[i] << ' \n ' ;
177
- Args.erase (Args.begin () + i);
178
- } else
179
- ++i;
180
- }
181
- OS << " ### Adding argument " << Edit << " at end\n " ;
182
- Args.push_back (GetStableCStr (SavedStrings, ' -' + Edit.str ()));
183
- } else {
184
- OS << " ### Unrecognized edit: " << Edit << " \n " ;
185
- }
186
- }
187
-
188
- // / ApplyQAOverride - Apply a comma separate list of edits to the
189
- // / input argument lists. See ApplyOneQAOverride.
190
- static void ApplyQAOverride (SmallVectorImpl<const char *> &Args,
191
- const char *OverrideStr,
192
- std::set<std::string> &SavedStrings) {
193
- raw_ostream *OS = &llvm::errs ();
194
-
195
- if (OverrideStr[0 ] == ' #' ) {
196
- ++OverrideStr;
197
- OS = &llvm::nulls ();
198
- }
199
-
200
- *OS << " ### CCC_OVERRIDE_OPTIONS: " << OverrideStr << " \n " ;
201
-
202
- // This does not need to be efficient.
203
-
204
- const char *S = OverrideStr;
205
- while (*S) {
206
- const char *End = ::strchr (S, ' ' );
207
- if (!End)
208
- End = S + strlen (S);
209
- if (End != S)
210
- ApplyOneQAOverride (*OS, Args, std::string (S, End), SavedStrings);
211
- S = End;
212
- if (*S != ' \0 ' )
213
- ++S;
214
- }
85
+ static const char *GetStableCStr (llvm::StringSet<> &SavedStrings, StringRef S) {
86
+ return SavedStrings.insert (S).first ->getKeyData ();
215
87
}
216
88
217
89
extern int cc1_main (ArrayRef<const char *> Argv, const char *Argv0,
@@ -230,7 +102,7 @@ extern int cc1gen_reproducer_main(ArrayRef<const char *> Argv,
230
102
231
103
static void insertTargetAndModeArgs (const ParsedClangName &NameParts,
232
104
SmallVectorImpl<const char *> &ArgVector,
233
- std::set<std::string > &SavedStrings) {
105
+ llvm::StringSet< > &SavedStrings) {
234
106
// Put target and mode arguments at the start of argument list so that
235
107
// arguments specified in command line could override them. Avoid putting
236
108
// them at index 0, as an option like '-cc1' must remain the first.
@@ -482,12 +354,13 @@ int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContext) {
482
354
}
483
355
}
484
356
485
- std::set<std::string > SavedStrings;
357
+ llvm::StringSet< > SavedStrings;
486
358
// Handle CCC_OVERRIDE_OPTIONS, used for editing a command line behind the
487
359
// scenes.
488
360
if (const char *OverrideStr = ::getenv (" CCC_OVERRIDE_OPTIONS" )) {
489
361
// FIXME: Driver shouldn't take extra initial argument.
490
- ApplyQAOverride (Args, OverrideStr, SavedStrings);
362
+ driver::applyOverrideOptions (Args, OverrideStr, SavedStrings,
363
+ &llvm::errs ());
491
364
}
492
365
493
366
std::string Path = GetExecutablePath (ToolContext.Path , CanonicalPrefixes);
0 commit comments