@@ -109,6 +109,21 @@ bool Option::matches(OptSpecifier Opt) const {
109109 return false ;
110110}
111111
112+ // / Advances to and returns the next argument string skipping
113+ // / newlines and empty lines (which are modeled by `nullptr`
114+ // / elements in the argument list).
115+ static const char *advanceToNextArgString (const ArgList &Args,
116+ unsigned &Index) {
117+ const unsigned int NumArgs = Args.getNumInputArgStrings ();
118+ while (Index <= NumArgs) {
119+ if (const char *ArgStr = Args.getArgString (Index - 1 )) {
120+ return ArgStr;
121+ }
122+ ++Index;
123+ }
124+ return nullptr ;
125+ }
126+
112127std::unique_ptr<Arg> Option::acceptInternal (const ArgList &Args,
113128 StringRef Spelling,
114129 unsigned &Index) const {
@@ -152,31 +167,40 @@ std::unique_ptr<Arg> Option::acceptInternal(const ArgList &Args,
152167
153168 return A;
154169 }
155- case SeparateClass:
170+ case SeparateClass: {
156171 // Matches iff this is an exact match.
157172 if (SpellingSize != ArgStringSize)
158173 return nullptr ;
159174
175+ unsigned StartIndex = Index;
160176 Index += 2 ;
161- if (Index > Args.getNumInputArgStrings () ||
162- Args.getArgString (Index - 1 ) == nullptr )
177+
178+ const char *ArgString = advanceToNextArgString (Args, Index);
179+ if (!ArgString)
163180 return nullptr ;
164181
165- return std::make_unique<Arg>(*this , Spelling, Index - 2 ,
166- Args. getArgString (Index - 1 ));
182+ return std::make_unique<Arg>(*this , Spelling, StartIndex, ArgString);
183+ }
167184 case MultiArgClass: {
168185 // Matches iff this is an exact match.
169186 if (SpellingSize != ArgStringSize)
170187 return nullptr ;
171188
172- Index += 1 + getNumArgs ();
173- if (Index > Args.getNumInputArgStrings ())
174- return nullptr ;
189+ unsigned StartIndex = Index;
190+ Index += 2 ;
191+
192+ SmallVector<const char *, 4 > Values;
193+ for (unsigned i = 0 ; i != getNumArgs (); ++i) {
194+ const char *ArgString = advanceToNextArgString (Args, Index);
195+ if (!ArgString)
196+ return nullptr ;
197+ Values.push_back (ArgString);
198+ ++Index;
199+ }
175200
176- auto A = std::make_unique<Arg>(*this , Spelling, Index - 1 - getNumArgs (),
177- Args.getArgString (Index - getNumArgs ()));
201+ auto A = std::make_unique<Arg>(*this , Spelling, StartIndex, Values[0 ]);
178202 for (unsigned i = 1 ; i != getNumArgs (); ++i)
179- A->getValues ().push_back (Args. getArgString (Index - getNumArgs () + i) );
203+ A->getValues ().push_back (Values[i] );
180204 return A;
181205 }
182206 case JoinedOrSeparateClass: {
@@ -187,32 +211,38 @@ std::unique_ptr<Arg> Option::acceptInternal(const ArgList &Args,
187211 }
188212
189213 // Otherwise it must be separate.
214+ unsigned StartIndex = Index;
190215 Index += 2 ;
191- if (Index > Args.getNumInputArgStrings () ||
192- Args.getArgString (Index - 1 ) == nullptr )
216+
217+ const char *ArgString = advanceToNextArgString (Args, Index);
218+ if (!ArgString)
193219 return nullptr ;
194220
195- return std::make_unique<Arg>(*this , Spelling, Index - 2 ,
196- Args.getArgString (Index - 1 ));
221+ return std::make_unique<Arg>(*this , Spelling, StartIndex, ArgString);
197222 }
198- case JoinedAndSeparateClass:
223+ case JoinedAndSeparateClass: {
199224 // Always matches.
225+ unsigned StartIndex = Index;
226+ const char *JoinedValue = Args.getArgString (StartIndex) + SpellingSize;
200227 Index += 2 ;
201- if (Index > Args.getNumInputArgStrings () ||
202- Args.getArgString (Index - 1 ) == nullptr )
228+
229+ const char *SeparateValue = advanceToNextArgString (Args, Index);
230+ if (!SeparateValue)
203231 return nullptr ;
204232
205- return std::make_unique<Arg>(*this , Spelling, Index - 2 ,
206- Args. getArgString (Index - 2 ) + SpellingSize,
207- Args. getArgString (Index - 1 ));
233+ return std::make_unique<Arg>(*this , Spelling, StartIndex, JoinedValue ,
234+ SeparateValue);
235+ }
208236 case RemainingArgsClass: {
209237 // Matches iff this is an exact match.
210238 if (SpellingSize != ArgStringSize)
211239 return nullptr ;
212- auto A = std::make_unique<Arg>(*this , Spelling, Index++);
213- while (Index < Args.getNumInputArgStrings () &&
214- Args.getArgString (Index) != nullptr )
215- A->getValues ().push_back (Args.getArgString (Index++));
240+ auto A = std::make_unique<Arg>(*this , Spelling, Index);
241+ Index += 2 ;
242+ while (const char *ArgString = advanceToNextArgString (Args, Index)) {
243+ A->getValues ().push_back (ArgString);
244+ Index++;
245+ }
216246 return A;
217247 }
218248 case RemainingArgsJoinedClass: {
@@ -221,10 +251,11 @@ std::unique_ptr<Arg> Option::acceptInternal(const ArgList &Args,
221251 // An inexact match means there is a joined arg.
222252 A->getValues ().push_back (Args.getArgString (Index) + SpellingSize);
223253 }
224- Index++;
225- while (Index < Args.getNumInputArgStrings () &&
226- Args.getArgString (Index) != nullptr )
227- A->getValues ().push_back (Args.getArgString (Index++));
254+ Index += 2 ;
255+ while (const char *ArgString = advanceToNextArgString (Args, Index)) {
256+ A->getValues ().push_back (ArgString);
257+ Index++;
258+ }
228259 return A;
229260 }
230261
0 commit comments