void parser<float>::anchor() {}
void parser<std::string>::anchor() {}
void parser<char>::anchor() {}
+void StringSaver::anchor() {}
//===----------------------------------------------------------------------===//
}
void cl::TokenizeGNUCommandLine(StringRef Src, StringSaver &Saver,
- SmallVectorImpl<const char *> &NewArgv) {
+ SmallVectorImpl<const char *> &NewArgv,
+ bool MarkEOLs) {
SmallString<128> Token;
for (size_t I = 0, E = Src.size(); I != E; ++I) {
// Consume runs of whitespace.
if (Token.empty()) {
- while (I != E && isWhitespace(Src[I]))
+ while (I != E && isWhitespace(Src[I])) {
+ // Mark the end of lines in response files
+ if (MarkEOLs && Src[I] == '\n')
+ NewArgv.push_back(nullptr);
++I;
+ }
if (I == E) break;
}
// End the token if this is whitespace.
if (isWhitespace(Src[I])) {
if (!Token.empty())
- NewArgv.push_back(Saver.saveCStr(Token.c_str()));
+ NewArgv.push_back(Saver.SaveString(Token.c_str()));
Token.clear();
continue;
}
// Append the last token after hitting EOF with no whitespace.
if (!Token.empty())
- NewArgv.push_back(Saver.saveCStr(Token.c_str()));
+ NewArgv.push_back(Saver.SaveString(Token.c_str()));
+ // Mark the end of response files
+ if (MarkEOLs)
+ NewArgv.push_back(nullptr);
}
/// Backslashes are interpreted in a rather complicated way in the Windows-style
}
void cl::TokenizeWindowsCommandLine(StringRef Src, StringSaver &Saver,
- SmallVectorImpl<const char *> &NewArgv) {
+ SmallVectorImpl<const char *> &NewArgv,
+ bool MarkEOLs) {
SmallString<128> Token;
// This is a small state machine to consume characters until it reaches the
// INIT state indicates that the current input index is at the start of
// the string or between tokens.
if (State == INIT) {
- if (isWhitespace(Src[I]))
+ if (isWhitespace(Src[I])) {
+ // Mark the end of lines in response files
+ if (MarkEOLs && Src[I] == '\n')
+ NewArgv.push_back(nullptr);
continue;
+ }
if (Src[I] == '"') {
State = QUOTED;
continue;
if (State == UNQUOTED) {
// Whitespace means the end of the token.
if (isWhitespace(Src[I])) {
- NewArgv.push_back(Saver.saveCStr(Token.c_str()));
+ NewArgv.push_back(Saver.SaveString(Token.c_str()));
Token.clear();
State = INIT;
+ // Mark the end of lines in response files
+ if (MarkEOLs && Src[I] == '\n')
+ NewArgv.push_back(nullptr);
continue;
}
if (Src[I] == '"') {
}
// Append the last token after hitting EOF with no whitespace.
if (!Token.empty())
- NewArgv.push_back(Saver.saveCStr(Token.c_str()));
+ NewArgv.push_back(Saver.SaveString(Token.c_str()));
+ // Mark the end of response files
+ if (MarkEOLs)
+ NewArgv.push_back(nullptr);
}
static bool ExpandResponseFile(const char *FName, StringSaver &Saver,
TokenizerCallback Tokenizer,
- SmallVectorImpl<const char *> &NewArgv) {
+ SmallVectorImpl<const char *> &NewArgv,
+ bool MarkEOLs = false) {
ErrorOr<std::unique_ptr<MemoryBuffer>> MemBufOrErr =
MemoryBuffer::getFile(FName);
if (!MemBufOrErr)
}
// Tokenize the contents into NewArgv.
- Tokenizer(Str, Saver, NewArgv);
+ Tokenizer(Str, Saver, NewArgv, MarkEOLs);
return true;
}
/// \brief Expand response files on a command line recursively using the given
/// StringSaver and tokenization strategy.
bool cl::ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer,
- SmallVectorImpl<const char *> &Argv) {
+ SmallVectorImpl<const char *> &Argv,
+ bool MarkEOLs) {
unsigned RspFiles = 0;
bool AllExpanded = true;
// Don't cache Argv.size() because it can change.
for (unsigned I = 0; I != Argv.size(); ) {
const char *Arg = Argv[I];
+ // Check if it is an EOL marker
+ if (Arg == nullptr) {
+ ++I;
+ continue;
+ }
if (Arg[0] != '@') {
++I;
continue;
// FIXME: If a nested response file uses a relative path, is it relative to
// the cwd of the process or the response file?
SmallVector<const char *, 0> ExpandedArgv;
- if (!ExpandResponseFile(Arg + 1, Saver, Tokenizer, ExpandedArgv)) {
+ if (!ExpandResponseFile(Arg + 1, Saver, Tokenizer, ExpandedArgv,
+ MarkEOLs)) {
// We couldn't read this file, so we leave it in the argument stream and
// move on.
AllExpanded = false;
return AllExpanded;
}
+namespace {
+ class StrDupSaver : public StringSaver {
+ std::vector<char*> Dups;
+ public:
+ ~StrDupSaver() {
+ for (std::vector<char *>::iterator I = Dups.begin(), E = Dups.end();
+ I != E; ++I) {
+ char *Dup = *I;
+ free(Dup);
+ }
+ }
+ const char *SaveString(const char *Str) override {
+ char *Dup = strdup(Str);
+ Dups.push_back(Dup);
+ return Dup;
+ }
+ };
+}
+
/// ParseEnvironmentOptions - An alternative entry point to the
/// CommandLine library, which allows you to read the program's name
/// from the caller (as PROGNAME) and its command-line arguments from
// Get program's "name", which we wouldn't know without the caller
// telling us.
SmallVector<const char *, 20> newArgv;
- StringSaver Saver;
- newArgv.push_back(Saver.saveCStr(progName));
+ StrDupSaver Saver;
+ newArgv.push_back(Saver.SaveString(progName));
// Parse the value of the environment variable into a "command line"
// and hand it off to ParseCommandLineOptions().
SmallVector<const char *, 20> newArgv;
for (int i = 0; i != argc; ++i)
newArgv.push_back(argv[i]);
- StringSaver Saver;
+ StrDupSaver Saver;
ExpandResponseFiles(Saver, TokenizeGNUCommandLine, newArgv);
argv = &newArgv[0];
argc = static_cast<int>(newArgv.size());