xref: /llvm-project/clang/unittests/Frontend/CompilerInvocationTest.cpp (revision 29125ddf1323951901184d2859274afdecac0327)
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/Frontend/CompilerInstance.h"
11 #include "llvm/Support/Host.h"
12 
13 #include "gmock/gmock.h"
14 #include "gtest/gtest.h"
15 
16 using namespace llvm;
17 using namespace clang;
18 
19 using ::testing::Contains;
20 using ::testing::Each;
21 using ::testing::StrEq;
22 using ::testing::StrNe;
23 
24 namespace {
25 
26 class CC1CommandLineGenerationTest : public ::testing::Test {
27 public:
28   IntrusiveRefCntPtr<DiagnosticsEngine> Diags;
29   SmallVector<const char *, 32> GeneratedArgs;
30   SmallVector<std::string, 32> GeneratedArgsStorage;
31 
32   const char *operator()(const Twine &Arg) {
33     return GeneratedArgsStorage.emplace_back(Arg.str()).c_str();
34   }
35 
36   CC1CommandLineGenerationTest()
37       : Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions())) {}
38 };
39 
40 TEST_F(CC1CommandLineGenerationTest, CanGenerateCC1CommandLineFlag) {
41   const char *Args[] = {"clang", "-xc++", "-fmodules-strict-context-hash", "-"};
42 
43   CompilerInvocation CInvok;
44   CompilerInvocation::CreateFromArgs(CInvok, Args, *Diags);
45 
46   CInvok.generateCC1CommandLine(GeneratedArgs, *this);
47 
48   ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fmodules-strict-context-hash")));
49 }
50 
51 TEST_F(CC1CommandLineGenerationTest, CanGenerateCC1CommandLineSeparate) {
52   const char *TripleCStr = "i686-apple-darwin9";
53   const char *Args[] = {"clang", "-xc++", "-triple", TripleCStr, "-"};
54 
55   CompilerInvocation CInvok;
56   CompilerInvocation::CreateFromArgs(CInvok, Args, *Diags);
57 
58   CInvok.generateCC1CommandLine(GeneratedArgs, *this);
59 
60   ASSERT_THAT(GeneratedArgs, Contains(StrEq(TripleCStr)));
61 }
62 
63 TEST_F(CC1CommandLineGenerationTest,
64        CanGenerateCC1CommandLineSeparateRequiredPresent) {
65   const std::string DefaultTriple =
66       llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple());
67   const char *Args[] = {"clang", "-xc++", "-triple", DefaultTriple.c_str(),
68                         "-"};
69 
70   CompilerInvocation CInvok;
71   CompilerInvocation::CreateFromArgs(CInvok, Args, *Diags);
72 
73   CInvok.generateCC1CommandLine(GeneratedArgs, *this);
74 
75   // Triple should always be emitted even if it is the default
76   ASSERT_THAT(GeneratedArgs, Contains(StrEq(DefaultTriple.c_str())));
77 }
78 
79 TEST_F(CC1CommandLineGenerationTest,
80        CanGenerateCC1CommandLineSeparateRequiredAbsent) {
81   const std::string DefaultTriple =
82       llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple());
83   const char *Args[] = {"clang", "-xc++", "-"};
84 
85   CompilerInvocation CInvok;
86   CompilerInvocation::CreateFromArgs(CInvok, Args, *Diags);
87 
88   CInvok.generateCC1CommandLine(GeneratedArgs, *this);
89 
90   // Triple should always be emitted even if it is the default
91   ASSERT_THAT(GeneratedArgs, Contains(StrEq(DefaultTriple.c_str())));
92 }
93 
94 TEST_F(CC1CommandLineGenerationTest, CanGenerateCC1CommandLineSeparateEnum) {
95   const char *RelocationModelCStr = "static";
96   const char *Args[] = {"clang", "-xc++", "-mrelocation-model",
97                         RelocationModelCStr, "-"};
98 
99   CompilerInvocation CInvok;
100   CompilerInvocation::CreateFromArgs(CInvok, Args, *Diags);
101 
102   CInvok.generateCC1CommandLine(GeneratedArgs, *this);
103 
104   // Non default relocation model
105   ASSERT_THAT(GeneratedArgs, Contains(StrEq(RelocationModelCStr)));
106   GeneratedArgs.clear();
107 
108   RelocationModelCStr = "pic";
109   Args[3] = RelocationModelCStr;
110 
111   CompilerInvocation CInvok1;
112   CompilerInvocation::CreateFromArgs(CInvok1, Args, *Diags);
113 
114   CInvok1.generateCC1CommandLine(GeneratedArgs, *this);
115   ASSERT_THAT(GeneratedArgs, Each(StrNe(RelocationModelCStr)));
116 }
117 
118 } // anonymous namespace
119