xref: /llvm-project/clang/unittests/Frontend/CompilerInvocationTest.cpp (revision afb4efd3bcc68ab95bf3c35183bedbdbf038356a)
1 //===- unittests/Frontend/CompilerInvocationTest.cpp - CI tests //---------===//
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 #include "clang/Frontend/CompilerInvocation.h"
10 #include "clang/Basic/TargetOptions.h"
11 #include "clang/Frontend/CompilerInstance.h"
12 #include "clang/Frontend/TextDiagnosticBuffer.h"
13 #include "clang/Lex/PreprocessorOptions.h"
14 #include "clang/Serialization/ModuleFileExtension.h"
15 #include "llvm/Support/Host.h"
16 
17 #include "gmock/gmock.h"
18 #include "gtest/gtest.h"
19 
20 using namespace llvm;
21 using namespace clang;
22 
23 using ::testing::Contains;
24 using ::testing::HasSubstr;
25 using ::testing::StrEq;
26 using ::testing::StartsWith;
27 
28 namespace {
29 class CommandLineTest : public ::testing::Test {
30 public:
31   IntrusiveRefCntPtr<DiagnosticsEngine> Diags;
32   SmallVector<const char *, 32> GeneratedArgs;
33   SmallVector<std::string, 32> GeneratedArgsStorage;
34   CompilerInvocation Invocation;
35 
36   const char *operator()(const Twine &Arg) {
37     return GeneratedArgsStorage.emplace_back(Arg.str()).c_str();
38   }
39 
40   CommandLineTest()
41       : Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions(),
42                                                   new TextDiagnosticBuffer())) {
43   }
44 };
45 
46 template <typename M>
47 std::string describeContainsN(M InnerMatcher, unsigned N, bool Negation) {
48   StringRef Contains = Negation ? "doesn't contain" : "contains";
49   StringRef Instance = N == 1 ? " instance " : " instances ";
50   StringRef Element = "of element that ";
51 
52   std::ostringstream Inner;
53   InnerMatcher.impl().DescribeTo(&Inner);
54 
55   return (Contains + " exactly " + Twine(N) + Instance + Element + Inner.str())
56       .str();
57 }
58 
59 MATCHER_P2(ContainsN, InnerMatcher, N,
60            describeContainsN(InnerMatcher, N, negation)) {
61   auto InnerMatches = [this](const auto &Element) {
62     ::testing::internal::DummyMatchResultListener InnerListener;
63     return InnerMatcher.impl().MatchAndExplain(Element, &InnerListener);
64   };
65 
66   return count_if(arg, InnerMatches) == N;
67 }
68 
69 TEST(ContainsN, Empty) {
70   const char *Array[] = {""};
71 
72   ASSERT_THAT(Array, ContainsN(StrEq("x"), 0));
73   ASSERT_THAT(Array, Not(ContainsN(StrEq("x"), 1)));
74   ASSERT_THAT(Array, Not(ContainsN(StrEq("x"), 2)));
75 }
76 
77 TEST(ContainsN, Zero) {
78   const char *Array[] = {"y"};
79 
80   ASSERT_THAT(Array, ContainsN(StrEq("x"), 0));
81   ASSERT_THAT(Array, Not(ContainsN(StrEq("x"), 1)));
82   ASSERT_THAT(Array, Not(ContainsN(StrEq("x"), 2)));
83 }
84 
85 TEST(ContainsN, One) {
86   const char *Array[] = {"a", "b", "x", "z"};
87 
88   ASSERT_THAT(Array, Not(ContainsN(StrEq("x"), 0)));
89   ASSERT_THAT(Array, ContainsN(StrEq("x"), 1));
90   ASSERT_THAT(Array, Not(ContainsN(StrEq("x"), 2)));
91 }
92 
93 TEST(ContainsN, Two) {
94   const char *Array[] = {"x", "a", "b", "x"};
95 
96   ASSERT_THAT(Array, Not(ContainsN(StrEq("x"), 0)));
97   ASSERT_THAT(Array, Not(ContainsN(StrEq("x"), 1)));
98   ASSERT_THAT(Array, ContainsN(StrEq("x"), 2));
99 }
100 
101 // Copy constructor/assignment perform deep copy of reference-counted pointers.
102 
103 TEST(CompilerInvocationTest, DeepCopyConstructor) {
104   CompilerInvocation A;
105   A.getAnalyzerOpts()->Config["Key"] = "Old";
106 
107   CompilerInvocation B(A);
108   B.getAnalyzerOpts()->Config["Key"] = "New";
109 
110   ASSERT_EQ(A.getAnalyzerOpts()->Config["Key"], "Old");
111 }
112 
113 TEST(CompilerInvocationTest, DeepCopyAssignment) {
114   CompilerInvocation A;
115   A.getAnalyzerOpts()->Config["Key"] = "Old";
116 
117   CompilerInvocation B;
118   B = A;
119   B.getAnalyzerOpts()->Config["Key"] = "New";
120 
121   ASSERT_EQ(A.getAnalyzerOpts()->Config["Key"], "Old");
122 }
123 
124 // Boolean option with a keypath that defaults to true.
125 // The only flag with a negative spelling can set the keypath to false.
126 
127 TEST_F(CommandLineTest, BoolOptionDefaultTrueSingleFlagNotPresent) {
128   const char *Args[] = {""};
129 
130   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
131   ASSERT_TRUE(Invocation.getFrontendOpts().UseTemporary);
132 
133   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
134 
135   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fno-temp-file"))));
136 }
137 
138 TEST_F(CommandLineTest, BoolOptionDefaultTrueSingleFlagPresent) {
139   const char *Args[] = {"-fno-temp-file"};
140 
141   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
142   ASSERT_FALSE(Invocation.getFrontendOpts().UseTemporary);
143 
144   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
145 
146   ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fno-temp-file")));
147 }
148 
149 TEST_F(CommandLineTest, CC1FlagPresentWhenDoingRoundTrip) {
150   const char *Args[] = {"-cc1", "-round-trip-args"};
151 
152   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
153 
154   ASSERT_THAT(std::string(Invocation.getCodeGenOpts().CmdArgs.begin(),
155                           Invocation.getCodeGenOpts().CmdArgs.end()),
156               StartsWith("-cc1"));
157 }
158 
159 TEST_F(CommandLineTest, CC1FlagPresentWhenNotDoingRoundTrip) {
160   const char *Args[] = {"-cc1", "-no-round-trip-args"};
161 
162   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
163 
164   ASSERT_THAT(std::string(Invocation.getCodeGenOpts().CmdArgs.begin(),
165                           Invocation.getCodeGenOpts().CmdArgs.end()),
166               StartsWith("-cc1"));
167 }
168 
169 TEST_F(CommandLineTest, BoolOptionDefaultTrueSingleFlagUnknownPresent) {
170   const char *Args[] = {"-ftemp-file"};
171 
172   // Driver-only flag.
173   ASSERT_FALSE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
174   ASSERT_TRUE(Invocation.getFrontendOpts().UseTemporary);
175 }
176 
177 // Boolean option with a keypath that defaults to true.
178 // The flag with negative spelling can set the keypath to false.
179 // The flag with positive spelling can reset the keypath to true.
180 
181 TEST_F(CommandLineTest, BoolOptionDefaultTruePresentNone) {
182   const char *Args[] = {""};
183 
184   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
185   ASSERT_TRUE(Invocation.getCodeGenOpts().Autolink);
186 
187   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
188   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fautolink"))));
189   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fno-autolink"))));
190 }
191 
192 TEST_F(CommandLineTest, BoolOptionDefaultTruePresentNegChange) {
193   const char *Args[] = {"-fno-autolink"};
194 
195   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
196   ASSERT_FALSE(Invocation.getCodeGenOpts().Autolink);
197 
198   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
199   ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fno-autolink")));
200   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fautolink"))));
201 }
202 
203 TEST_F(CommandLineTest, BoolOptionDefaultTruePresentPosReset) {
204   const char *Args[] = {"-fautolink"};
205 
206   // Driver-only flag.
207   ASSERT_FALSE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
208   ASSERT_TRUE(Invocation.getCodeGenOpts().Autolink);
209 }
210 
211 // Boolean option with a keypath that defaults to false.
212 // The flag with negative spelling can set the keypath to true.
213 // The flag with positive spelling can reset the keypath to false.
214 
215 TEST_F(CommandLineTest, BoolOptionDefaultFalsePresentNone) {
216   const char *Args[] = {""};
217 
218   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
219   ASSERT_FALSE(Invocation.getCodeGenOpts().NoInlineLineTables);
220 
221   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
222   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-ginline-line-tables"))));
223   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-gno-inline-line-tables"))));
224 }
225 
226 TEST_F(CommandLineTest, BoolOptionDefaultFalsePresentNegChange) {
227   const char *Args[] = {"-gno-inline-line-tables"};
228 
229   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
230   ASSERT_TRUE(Invocation.getCodeGenOpts().NoInlineLineTables);
231 
232   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
233   ASSERT_THAT(GeneratedArgs, Contains(StrEq("-gno-inline-line-tables")));
234   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-ginline-line-tables"))));
235 }
236 
237 TEST_F(CommandLineTest, BoolOptionDefaultFalsePresentPosReset) {
238   const char *Args[] = {"-ginline-line-tables"};
239 
240   // Driver-only flag.
241   ASSERT_FALSE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
242   ASSERT_FALSE(Invocation.getCodeGenOpts().NoInlineLineTables);
243 }
244 
245 // Boolean option with a keypath that defaults to false.
246 // The flag with positive spelling can set the keypath to true.
247 // The flag with negative spelling can reset the keypath to false.
248 
249 TEST_F(CommandLineTest, BoolOptionDefaultFalsePresentNoneX) {
250   const char *Args[] = {""};
251 
252   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
253   ASSERT_FALSE(Invocation.getCodeGenOpts().CodeViewGHash);
254 
255   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
256   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-gcodeview-ghash"))));
257   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-gno-codeview-ghash"))));
258 }
259 
260 TEST_F(CommandLineTest, BoolOptionDefaultFalsePresentPosChange) {
261   const char *Args[] = {"-gcodeview-ghash"};
262 
263   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
264   ASSERT_TRUE(Invocation.getCodeGenOpts().CodeViewGHash);
265 
266   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
267   ASSERT_THAT(GeneratedArgs, Contains(StrEq("-gcodeview-ghash")));
268   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-gno-codeview-ghash"))));
269 }
270 
271 TEST_F(CommandLineTest, BoolOptionDefaultFalsePresentNegReset) {
272   const char *Args[] = {"-gno-codeview-ghash"};
273 
274   // Driver-only flag.
275   ASSERT_FALSE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
276   ASSERT_FALSE(Invocation.getCodeGenOpts().CodeViewGHash);
277 }
278 
279 // Boolean option with a keypath that defaults to an arbitrary expression.
280 // The flag with positive spelling can set the keypath to true.
281 // The flag with negative spelling can set the keypath to false.
282 
283 TEST_F(CommandLineTest, BoolOptionDefaultArbitraryTwoFlagsPresentNone) {
284   const char *Args = {""};
285 
286   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
287   ASSERT_EQ(Invocation.getCodeGenOpts().ClearASTBeforeBackend, false);
288 
289   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
290 
291   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-no-clear-ast-before-backend"))));
292   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-clear-ast-before-backend"))));
293 }
294 
295 TEST_F(CommandLineTest, BoolOptionDefaultArbitraryTwoFlagsPresentChange) {
296   const char *Args[] = {"-clear-ast-before-backend"};
297 
298   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
299   ASSERT_EQ(Invocation.getCodeGenOpts().ClearASTBeforeBackend, true);
300 
301   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
302   ASSERT_THAT(GeneratedArgs, Contains(StrEq("-clear-ast-before-backend")));
303   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-no-clear-ast-before-backend"))));
304 }
305 
306 TEST_F(CommandLineTest, BoolOptionDefaultArbitraryTwoFlagsPresentReset) {
307   const char *Args[] = {"-no-clear-ast-before-backend"};
308 
309   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
310   ASSERT_EQ(Invocation.getCodeGenOpts().ClearASTBeforeBackend, false);
311 
312   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
313   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-no-clear-ast-before-backend"))));
314   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-clear-ast-before-backend"))));
315 }
316 
317 // Boolean option that gets the CC1Option flag from a let statement (which
318 // is applied **after** the record is defined):
319 //
320 //   let Flags = [CC1Option] in {
321 //     defm option : BoolOption<...>;
322 //   }
323 
324 TEST_F(CommandLineTest, BoolOptionCC1ViaLetPresentNone) {
325   const char *Args[] = {""};
326 
327   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
328   ASSERT_FALSE(Invocation.getCodeGenOpts().DebugPassManager);
329 
330   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
331 
332   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fdebug-pass-manager"))));
333   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fno-debug-pass-manager"))));
334 }
335 
336 TEST_F(CommandLineTest, BoolOptionCC1ViaLetPresentPos) {
337   const char *Args[] = {"-fdebug-pass-manager"};
338 
339   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
340   ASSERT_TRUE(Invocation.getCodeGenOpts().DebugPassManager);
341 
342   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
343 
344   ASSERT_THAT(GeneratedArgs, ContainsN(StrEq("-fdebug-pass-manager"), 1));
345   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fno-debug-pass-manager"))));
346 }
347 
348 TEST_F(CommandLineTest, BoolOptionCC1ViaLetPresentNeg) {
349   const char *Args[] = {"-fno-debug-pass-manager"};
350 
351   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
352   ASSERT_FALSE(Invocation.getCodeGenOpts().DebugPassManager);
353 
354   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
355 
356   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fno-debug-pass-manager"))));
357   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fdebug-pass-manager"))));
358 }
359 
360 TEST_F(CommandLineTest, CanGenerateCC1CommandLineFlag) {
361   const char *Args[] = {"-fmodules-strict-context-hash"};
362 
363   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
364 
365   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
366 
367   ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fmodules-strict-context-hash")));
368 }
369 
370 TEST_F(CommandLineTest, CanGenerateCC1CommandLineSeparate) {
371   const char *TripleCStr = "i686-apple-darwin9";
372   const char *Args[] = {"-triple", TripleCStr};
373 
374   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
375 
376   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
377 
378   ASSERT_THAT(GeneratedArgs, Contains(StrEq(TripleCStr)));
379 }
380 
381 TEST_F(CommandLineTest,  CanGenerateCC1CommandLineSeparateRequiredPresent) {
382   const std::string DefaultTriple =
383       llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple());
384   const char *Args[] = {"-triple", DefaultTriple.c_str()};
385 
386   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
387 
388   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
389 
390   // Triple should always be emitted even if it is the default
391   ASSERT_THAT(GeneratedArgs, Contains(StrEq(DefaultTriple.c_str())));
392 }
393 
394 TEST_F(CommandLineTest, CanGenerateCC1CommandLineSeparateRequiredAbsent) {
395   const std::string DefaultTriple =
396       llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple());
397   const char *Args[] = {""};
398 
399   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
400 
401   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
402 
403   // Triple should always be emitted even if it is the default
404   ASSERT_THAT(GeneratedArgs, Contains(StrEq(DefaultTriple.c_str())));
405 }
406 
407 TEST_F(CommandLineTest, SeparateEnumNonDefault) {
408   const char *Args[] = {"-mrelocation-model", "static"};
409 
410   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
411   ASSERT_EQ(Invocation.getCodeGenOpts().RelocationModel, Reloc::Model::Static);
412 
413   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
414 
415   // Non default relocation model.
416   ASSERT_THAT(GeneratedArgs, Contains(StrEq("-mrelocation-model")));
417   ASSERT_THAT(GeneratedArgs, Contains(StrEq("static")));
418   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-mrelocation-model=static"))));
419 }
420 
421 TEST_F(CommandLineTest, SeparateEnumDefault) {
422   const char *Args[] = {"-mrelocation-model", "pic"};
423 
424   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
425   ASSERT_EQ(Invocation.getCodeGenOpts().RelocationModel, Reloc::Model::PIC_);
426 
427   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
428 
429   // Default relocation model.
430   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-mrelocation-model"))));
431   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("pic"))));
432   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-mrelocation-model=pic"))));
433 }
434 
435 TEST_F(CommandLineTest, JoinedEnumNonDefault) {
436   const char *Args[] = {"-fobjc-dispatch-method=non-legacy"};
437 
438   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
439   ASSERT_EQ(Invocation.getCodeGenOpts().getObjCDispatchMethod(),
440             CodeGenOptions::NonLegacy);
441 
442   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
443 
444   ASSERT_THAT(GeneratedArgs,
445               Contains(StrEq("-fobjc-dispatch-method=non-legacy")));
446   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fobjc-dispatch-method="))));
447   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("non-legacy"))));
448 }
449 
450 TEST_F(CommandLineTest, JoinedEnumDefault) {
451   const char *Args[] = {"-fobjc-dispatch-method=legacy"};
452 
453   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
454   ASSERT_EQ(Invocation.getCodeGenOpts().getObjCDispatchMethod(),
455             CodeGenOptions::Legacy);
456 
457   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
458 
459   ASSERT_THAT(GeneratedArgs,
460               Not(Contains(StrEq("-fobjc-dispatch-method=legacy"))));
461   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fobjc-dispatch-method="))));
462   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("legacy"))));
463 }
464 
465 TEST_F(CommandLineTest, StringVectorEmpty) {
466   const char *Args[] = {""};
467 
468   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
469   ASSERT_TRUE(Invocation.getFrontendOpts().ModuleMapFiles.empty());
470 
471   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
472 
473   ASSERT_THAT(GeneratedArgs, Not(Contains(HasSubstr("-fmodule-map-file"))));
474 }
475 
476 TEST_F(CommandLineTest, StringVectorSingle) {
477   const char *Args[] = {"-fmodule-map-file=a"};
478 
479   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
480   ASSERT_EQ(Invocation.getFrontendOpts().ModuleMapFiles,
481             std::vector<std::string>({"a"}));
482 
483   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
484 
485   ASSERT_THAT(GeneratedArgs, ContainsN(StrEq("-fmodule-map-file=a"), 1));
486   ASSERT_THAT(GeneratedArgs, ContainsN(HasSubstr("-fmodule-map-file"), 1));
487 }
488 
489 TEST_F(CommandLineTest, StringVectorMultiple) {
490   const char *Args[] = {"-fmodule-map-file=a", "-fmodule-map-file=b"};
491 
492   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
493   ASSERT_TRUE(Invocation.getFrontendOpts().ModuleMapFiles ==
494               std::vector<std::string>({"a", "b"}));
495 
496   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
497 
498   ASSERT_THAT(GeneratedArgs, ContainsN(StrEq("-fmodule-map-file=a"), 1));
499   ASSERT_THAT(GeneratedArgs, ContainsN(StrEq("-fmodule-map-file=b"), 1));
500   ASSERT_THAT(GeneratedArgs, ContainsN(HasSubstr("-fmodule-map-file"), 2));
501 }
502 
503 // CommaJoined option with MarshallingInfoStringVector.
504 
505 TEST_F(CommandLineTest, StringVectorCommaJoinedNone) {
506   const char *Args[] = {""};
507 
508   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
509   ASSERT_TRUE(Invocation.getLangOpts()->CommentOpts.BlockCommandNames.empty());
510 
511   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
512 
513   ASSERT_THAT(GeneratedArgs,
514               Not(Contains(HasSubstr("-fcomment-block-commands"))));
515 }
516 
517 TEST_F(CommandLineTest, StringVectorCommaJoinedSingle) {
518   const char *Args[] = {"-fcomment-block-commands=x,y"};
519 
520   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
521   ASSERT_EQ(Invocation.getLangOpts()->CommentOpts.BlockCommandNames,
522             std::vector<std::string>({"x", "y"}));
523 
524   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
525 
526   ASSERT_THAT(GeneratedArgs,
527               ContainsN(StrEq("-fcomment-block-commands=x,y"), 1));
528 }
529 
530 TEST_F(CommandLineTest, StringVectorCommaJoinedMultiple) {
531   const char *Args[] = {"-fcomment-block-commands=x,y",
532                         "-fcomment-block-commands=a,b"};
533 
534   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
535   ASSERT_EQ(Invocation.getLangOpts()->CommentOpts.BlockCommandNames,
536             std::vector<std::string>({"x", "y", "a", "b"}));
537 
538   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
539 
540   ASSERT_THAT(GeneratedArgs,
541               ContainsN(StrEq("-fcomment-block-commands=x,y,a,b"), 1));
542 }
543 
544 // A flag that should be parsed only if a condition is met.
545 
546 TEST_F(CommandLineTest, ConditionalParsingIfFalseFlagNotPresent) {
547   const char *Args[] = {""};
548 
549   CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
550 
551   ASSERT_FALSE(Diags->hasErrorOccurred());
552   ASSERT_FALSE(Invocation.getLangOpts()->SYCLIsDevice);
553   ASSERT_FALSE(Invocation.getLangOpts()->SYCLIsHost);
554   ASSERT_EQ(Invocation.getLangOpts()->getSYCLVersion(), LangOptions::SYCL_None);
555 
556   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
557 
558   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fsycl"))));
559   ASSERT_THAT(GeneratedArgs, Not(Contains(HasSubstr("-sycl-std="))));
560 }
561 
562 TEST_F(CommandLineTest, ConditionalParsingIfFalseFlagPresent) {
563   const char *Args[] = {"-sycl-std=2017"};
564 
565   CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
566 
567   ASSERT_FALSE(Diags->hasErrorOccurred());
568   ASSERT_FALSE(Invocation.getLangOpts()->SYCLIsDevice);
569   ASSERT_FALSE(Invocation.getLangOpts()->SYCLIsHost);
570   ASSERT_EQ(Invocation.getLangOpts()->getSYCLVersion(), LangOptions::SYCL_None);
571 
572   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
573 
574   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fsycl-is-device"))));
575   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fsycl-is-host"))));
576   ASSERT_THAT(GeneratedArgs, Not(Contains(HasSubstr("-sycl-std="))));
577 }
578 
579 TEST_F(CommandLineTest, ConditionalParsingIfNonsenseSyclStdArg) {
580   const char *Args[] = {"-fsycl-is-device", "-sycl-std=garbage"};
581 
582   CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
583 
584   ASSERT_TRUE(Diags->hasErrorOccurred());
585   ASSERT_TRUE(Invocation.getLangOpts()->SYCLIsDevice);
586   ASSERT_FALSE(Invocation.getLangOpts()->SYCLIsHost);
587   ASSERT_EQ(Invocation.getLangOpts()->getSYCLVersion(), LangOptions::SYCL_None);
588 
589   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
590 
591   ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fsycl-is-device")));
592   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fsycl-is-host"))));
593   ASSERT_THAT(GeneratedArgs, Not(Contains(HasSubstr("-sycl-std="))));
594 }
595 
596 TEST_F(CommandLineTest, ConditionalParsingIfOddSyclStdArg1) {
597   const char *Args[] = {"-fsycl-is-device", "-sycl-std=121"};
598 
599   CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
600 
601   ASSERT_FALSE(Diags->hasErrorOccurred());
602   ASSERT_TRUE(Invocation.getLangOpts()->SYCLIsDevice);
603   ASSERT_FALSE(Invocation.getLangOpts()->SYCLIsHost);
604   ASSERT_EQ(Invocation.getLangOpts()->getSYCLVersion(), LangOptions::SYCL_2017);
605 
606   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
607 
608   ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fsycl-is-device")));
609   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fsycl-is-host"))));
610   ASSERT_THAT(GeneratedArgs, Contains(HasSubstr("-sycl-std=2017")));
611 }
612 
613 TEST_F(CommandLineTest, ConditionalParsingIfOddSyclStdArg2) {
614   const char *Args[] = {"-fsycl-is-device", "-sycl-std=1.2.1"};
615 
616   CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
617 
618   ASSERT_FALSE(Diags->hasErrorOccurred());
619   ASSERT_TRUE(Invocation.getLangOpts()->SYCLIsDevice);
620   ASSERT_FALSE(Invocation.getLangOpts()->SYCLIsHost);
621   ASSERT_EQ(Invocation.getLangOpts()->getSYCLVersion(), LangOptions::SYCL_2017);
622 
623   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
624 
625   ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fsycl-is-device")));
626   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fsycl-is-host"))));
627   ASSERT_THAT(GeneratedArgs, Contains(HasSubstr("-sycl-std=2017")));
628 }
629 
630 TEST_F(CommandLineTest, ConditionalParsingIfOddSyclStdArg3) {
631   const char *Args[] = {"-fsycl-is-device", "-sycl-std=sycl-1.2.1"};
632 
633   CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
634 
635   ASSERT_FALSE(Diags->hasErrorOccurred());
636   ASSERT_TRUE(Invocation.getLangOpts()->SYCLIsDevice);
637   ASSERT_FALSE(Invocation.getLangOpts()->SYCLIsHost);
638   ASSERT_EQ(Invocation.getLangOpts()->getSYCLVersion(), LangOptions::SYCL_2017);
639 
640   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
641 
642   ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fsycl-is-device")));
643   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fsycl-is-host"))));
644   ASSERT_THAT(GeneratedArgs, Contains(HasSubstr("-sycl-std=2017")));
645 }
646 
647 TEST_F(CommandLineTest, ConditionalParsingIfTrueFlagNotPresentHost) {
648   const char *Args[] = {"-fsycl-is-host"};
649 
650   CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
651 
652   ASSERT_FALSE(Diags->hasErrorOccurred());
653   ASSERT_EQ(Invocation.getLangOpts()->getSYCLVersion(),
654             LangOptions::SYCL_Default);
655 
656   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
657 
658   ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fsycl-is-host")));
659   ASSERT_THAT(GeneratedArgs, Contains(HasSubstr("-sycl-std=")));
660 }
661 
662 TEST_F(CommandLineTest, ConditionalParsingIfTrueFlagNotPresentDevice) {
663   const char *Args[] = {"-fsycl-is-device"};
664 
665   CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
666 
667   ASSERT_FALSE(Diags->hasErrorOccurred());
668   ASSERT_EQ(Invocation.getLangOpts()->getSYCLVersion(),
669             LangOptions::SYCL_Default);
670 
671   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
672 
673   ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fsycl-is-device")));
674   ASSERT_THAT(GeneratedArgs, Contains(HasSubstr("-sycl-std=")));
675 }
676 
677 TEST_F(CommandLineTest, ConditionalParsingIfTrueFlagPresent) {
678   const char *Args[] = {"-fsycl-is-device", "-sycl-std=2017"};
679 
680   CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
681 
682   ASSERT_FALSE(Diags->hasErrorOccurred());
683   ASSERT_EQ(Invocation.getLangOpts()->getSYCLVersion(), LangOptions::SYCL_2017);
684 
685   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
686 
687   ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fsycl-is-device")));
688   ASSERT_THAT(GeneratedArgs, Contains(StrEq("-sycl-std=2017")));
689 }
690 
691 // Wide integer option.
692 
693 TEST_F(CommandLineTest, WideIntegerHighValue) {
694   const char *Args[] = {"-fbuild-session-timestamp=1609827494445723662"};
695 
696   CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
697 
698   ASSERT_FALSE(Diags->hasErrorOccurred());
699   ASSERT_EQ(Invocation.getHeaderSearchOpts().BuildSessionTimestamp,
700             1609827494445723662ull);
701 }
702 
703 // Tree of boolean options that can be (directly or transitively) implied by
704 // their parent:
705 //
706 //   * -cl-unsafe-math-optimizations
707 //     * -cl-mad-enable
708 //     * -menable-unsafe-fp-math
709 //       * -freciprocal-math
710 
711 TEST_F(CommandLineTest, ImpliedBoolOptionsNoFlagPresent) {
712   const char *Args[] = {""};
713 
714   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
715   ASSERT_FALSE(Invocation.getLangOpts()->CLUnsafeMath);
716   ASSERT_FALSE(Invocation.getCodeGenOpts().LessPreciseFPMAD);
717   ASSERT_FALSE(Invocation.getLangOpts()->UnsafeFPMath);
718   ASSERT_FALSE(Invocation.getLangOpts()->AllowRecip);
719 
720   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
721 
722   // Not generated - missing.
723   ASSERT_THAT(GeneratedArgs,
724               Not(Contains(StrEq("-cl-unsafe-math-optimizations"))));
725   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-cl-mad-enable"))));
726   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-menable-unsafe-fp-math"))));
727   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-freciprocal-math"))));
728 }
729 
730 TEST_F(CommandLineTest, ImpliedBoolOptionsRootFlagPresent) {
731   const char *Args[] = {"-cl-unsafe-math-optimizations"};
732 
733   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
734   // Explicitly provided root flag.
735   ASSERT_TRUE(Invocation.getLangOpts()->CLUnsafeMath);
736   // Directly implied by explicitly provided root flag.
737   ASSERT_TRUE(Invocation.getCodeGenOpts().LessPreciseFPMAD);
738   ASSERT_TRUE(Invocation.getLangOpts()->UnsafeFPMath);
739   // Transitively implied by explicitly provided root flag.
740   ASSERT_TRUE(Invocation.getLangOpts()->AllowRecip);
741 
742   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
743 
744   // Generated - explicitly provided.
745   ASSERT_THAT(GeneratedArgs, Contains(StrEq("-cl-unsafe-math-optimizations")));
746   // Not generated - implied by the generated root flag.
747   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-cl-mad-enable"))));
748   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-menable-unsafe-fp-math"))));
749   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-freciprocal-math"))));
750 }
751 
752 TEST_F(CommandLineTest, ImpliedBoolOptionsAllFlagsPresent) {
753   const char *Args[] = {"-cl-unsafe-math-optimizations", "-cl-mad-enable",
754                         "-menable-unsafe-fp-math", "-freciprocal-math"};
755 
756   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
757   ASSERT_TRUE(Invocation.getLangOpts()->CLUnsafeMath);
758   ASSERT_TRUE(Invocation.getCodeGenOpts().LessPreciseFPMAD);
759   ASSERT_TRUE(Invocation.getLangOpts()->UnsafeFPMath);
760   ASSERT_TRUE(Invocation.getLangOpts()->AllowRecip);
761 
762   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
763 
764   // Generated - explicitly provided.
765   ASSERT_THAT(GeneratedArgs, Contains(StrEq("-cl-unsafe-math-optimizations")));
766   // Not generated - implied by their generated parent.
767   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-cl-mad-enable"))));
768   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-menable-unsafe-fp-math"))));
769   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-freciprocal-math"))));
770 }
771 
772 TEST_F(CommandLineTest, ImpliedBoolOptionsImpliedFlagsPresent) {
773   const char *Args[] = {"-cl-mad-enable", "-menable-unsafe-fp-math",
774                         "-freciprocal-math"};
775 
776   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
777   ASSERT_FALSE(Invocation.getLangOpts()->CLUnsafeMath);
778   ASSERT_TRUE(Invocation.getCodeGenOpts().LessPreciseFPMAD);
779   ASSERT_TRUE(Invocation.getLangOpts()->UnsafeFPMath);
780   ASSERT_TRUE(Invocation.getLangOpts()->AllowRecip);
781 
782   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
783   // Not generated - missing.
784   ASSERT_THAT(GeneratedArgs,
785               Not(Contains(StrEq("-cl-unsafe-math-optimizations"))));
786   // Generated - explicitly provided.
787   ASSERT_THAT(GeneratedArgs, Contains(StrEq("-cl-mad-enable")));
788   ASSERT_THAT(GeneratedArgs, Contains(StrEq("-menable-unsafe-fp-math")));
789   // Not generated - implied by its generated parent.
790   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-freciprocal-math"))));
791 }
792 
793 TEST_F(CommandLineTest, PresentAndNotImpliedGenerated) {
794   const char *Args[] = {"-cl-mad-enable", "-menable-unsafe-fp-math"};
795 
796   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
797 
798   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
799 
800   // Present options that were not implied are generated.
801   ASSERT_THAT(GeneratedArgs, Contains(StrEq("-cl-mad-enable")));
802   ASSERT_THAT(GeneratedArgs, Contains(StrEq("-menable-unsafe-fp-math")));
803 }
804 
805 // Diagnostic option.
806 
807 TEST_F(CommandLineTest, DiagnosticOptionPresent) {
808   const char *Args[] = {"-verify=xyz"};
809 
810   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
811 
812   ASSERT_EQ(Invocation.getDiagnosticOpts().VerifyPrefixes,
813             std::vector<std::string>({"xyz"}));
814 
815   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
816 
817   ASSERT_THAT(GeneratedArgs, ContainsN(StrEq("-verify=xyz"), 1));
818 }
819 
820 // Option default depends on language standard.
821 
822 TEST_F(CommandLineTest, DigraphsImplied) {
823   const char *Args[] = {""};
824 
825   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
826   ASSERT_TRUE(Invocation.getLangOpts()->Digraphs);
827 
828   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
829   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fno-digraphs"))));
830   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fdigraphs"))));
831 }
832 
833 TEST_F(CommandLineTest, DigraphsDisabled) {
834   const char *Args[] = {"-fno-digraphs"};
835 
836   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
837   ASSERT_FALSE(Invocation.getLangOpts()->Digraphs);
838 
839   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
840   ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fno-digraphs")));
841   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fdigraphs"))));
842 }
843 
844 TEST_F(CommandLineTest, DigraphsNotImplied) {
845   const char *Args[] = {"-std=c89"};
846 
847   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
848   ASSERT_FALSE(Invocation.getLangOpts()->Digraphs);
849 
850   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
851   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fno-digraphs"))));
852   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fdigraphs"))));
853 }
854 
855 TEST_F(CommandLineTest, DigraphsEnabled) {
856   const char *Args[] = {"-std=c89", "-fdigraphs"};
857 
858   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
859   ASSERT_TRUE(Invocation.getLangOpts()->Digraphs);
860 
861   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
862   ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fdigraphs")));
863 }
864 
865 struct DummyModuleFileExtension
866     : public llvm::RTTIExtends<DummyModuleFileExtension, ModuleFileExtension> {
867   static char ID;
868 
869   ModuleFileExtensionMetadata getExtensionMetadata() const override {
870     return {};
871   };
872 
873   void hashExtension(ExtensionHashBuilder &HBuilder) const override {}
874 
875   std::unique_ptr<ModuleFileExtensionWriter>
876   createExtensionWriter(ASTWriter &Writer) override {
877     return {};
878   }
879 
880   std::unique_ptr<ModuleFileExtensionReader>
881   createExtensionReader(const ModuleFileExtensionMetadata &Metadata,
882                         ASTReader &Reader, serialization::ModuleFile &Mod,
883                         const llvm::BitstreamCursor &Stream) override {
884     return {};
885   }
886 };
887 
888 char DummyModuleFileExtension::ID = 0;
889 
890 TEST_F(CommandLineTest, TestModuleFileExtension) {
891   const char *Args[] = {"-ftest-module-file-extension=first:2:1:0:first",
892                         "-ftest-module-file-extension=second:3:2:1:second"};
893 
894   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
895   ASSERT_THAT(Invocation.getFrontendOpts().ModuleFileExtensions.size(), 2);
896 
897   // Exercise the check that only serializes instances of
898   // TestModuleFileExtension by providing an instance of another
899   // ModuleFileExtension subclass.
900   Invocation.getFrontendOpts().ModuleFileExtensions.push_back(
901       std::make_shared<DummyModuleFileExtension>());
902 
903   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
904 
905   ASSERT_THAT(GeneratedArgs,
906               ContainsN(HasSubstr("-ftest-module-file-extension="), 2));
907   ASSERT_THAT(
908       GeneratedArgs,
909       Contains(StrEq("-ftest-module-file-extension=first:2:1:0:first")));
910   ASSERT_THAT(
911       GeneratedArgs,
912       Contains(StrEq("-ftest-module-file-extension=second:3:2:1:second")));
913 }
914 
915 TEST_F(CommandLineTest, RoundTrip) {
916   // Testing one marshalled and one manually generated option from each
917   // CompilerInvocation member.
918   const char *Args[] = {
919       "-round-trip-args",
920       // LanguageOptions
921       "-std=c17",
922       "-fmax-tokens=10",
923       // TargetOptions
924       "-target-sdk-version=1.2.3",
925       "-meabi",
926       "4",
927       // DiagnosticOptions
928       "-Wundef-prefix=XY",
929       "-fdiagnostics-format",
930       "clang",
931       // HeaderSearchOptions
932       "-stdlib=libc++",
933       "-fimplicit-module-maps",
934       // PreprocessorOptions
935       "-DXY=AB",
936       "-include-pch",
937       "a.pch",
938       // AnalyzerOptions
939       "-analyzer-config",
940       "ctu-import-threshold=42",
941       "-unoptimized-cfg",
942       // MigratorOptions (no manually handled arguments)
943       "-no-ns-alloc-error",
944       // CodeGenOptions
945       "-debug-info-kind=limited",
946       "-debug-info-macro",
947       // DependencyOutputOptions
948       "--show-includes",
949       "-H",
950       // FileSystemOptions (no manually handled arguments)
951       "-working-directory",
952       "folder",
953       // FrontendOptions
954       "-load",
955       "plugin",
956       "-ast-merge",
957       // PreprocessorOutputOptions
958       "-dD",
959       "-CC",
960   };
961 
962   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
963 
964   ASSERT_TRUE(Invocation.getLangOpts()->C17);
965   ASSERT_EQ(Invocation.getLangOpts()->MaxTokens, 10u);
966 
967   ASSERT_EQ(Invocation.getTargetOpts().SDKVersion, llvm::VersionTuple(1, 2, 3));
968   ASSERT_EQ(Invocation.getTargetOpts().EABIVersion, EABI::EABI4);
969 
970   ASSERT_THAT(Invocation.getDiagnosticOpts().UndefPrefixes,
971               Contains(StrEq("XY")));
972   ASSERT_EQ(Invocation.getDiagnosticOpts().getFormat(),
973             TextDiagnosticFormat::Clang);
974 
975   ASSERT_TRUE(Invocation.getHeaderSearchOpts().UseLibcxx);
976   ASSERT_TRUE(Invocation.getHeaderSearchOpts().ImplicitModuleMaps);
977 
978   ASSERT_THAT(Invocation.getPreprocessorOpts().Macros,
979               Contains(std::make_pair(std::string("XY=AB"), false)));
980   ASSERT_EQ(Invocation.getPreprocessorOpts().ImplicitPCHInclude, "a.pch");
981 
982   ASSERT_EQ(Invocation.getAnalyzerOpts()->Config["ctu-import-threshold"], "42");
983   ASSERT_TRUE(Invocation.getAnalyzerOpts()->UnoptimizedCFG);
984 
985   ASSERT_TRUE(Invocation.getMigratorOpts().NoNSAllocReallocError);
986 
987   ASSERT_EQ(Invocation.getCodeGenOpts().getDebugInfo(),
988             codegenoptions::DebugInfoKind::LimitedDebugInfo);
989   ASSERT_TRUE(Invocation.getCodeGenOpts().MacroDebugInfo);
990 
991   ASSERT_EQ(Invocation.getDependencyOutputOpts().ShowIncludesDest,
992             ShowIncludesDestination::Stdout);
993   ASSERT_TRUE(Invocation.getDependencyOutputOpts().ShowHeaderIncludes);
994 }
995 
996 TEST_F(CommandLineTest, PluginArgsRoundTripDeterminism) {
997   const char *Args[] = {
998       "-plugin-arg-blink-gc-plugin", "no-members-in-stack-allocated",
999       "-plugin-arg-find-bad-constructs", "checked-ptr-as-trivial-member",
1000       "-plugin-arg-find-bad-constructs", "check-ipc",
1001       // Enable round-trip to ensure '-plugin-arg' generation is deterministic.
1002       "-round-trip-args"};
1003 
1004   ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags));
1005 }
1006 } // anonymous namespace
1007