Skip to content

Commit

Permalink
[clang][cli] CompilerInvocationTest: join two test fixtures into one
Browse files Browse the repository at this point in the history
Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D92825
  • Loading branch information
jansvoboda11 committed Dec 9, 2020
1 parent 384383e commit cf2bb22
Showing 1 changed file with 17 additions and 37 deletions.
54 changes: 17 additions & 37 deletions clang/unittests/Frontend/CompilerInvocationTest.cpp
Expand Up @@ -22,45 +22,38 @@ using ::testing::StrEq;
using ::testing::StrNe;

namespace {
struct OptsPopulationTest : public ::testing::Test {
IntrusiveRefCntPtr<DiagnosticsEngine> Diags;
CompilerInvocation CInvok;

OptsPopulationTest()
: Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions())) {}
};

class CC1CommandLineGenerationTest : public ::testing::Test {
class CommandLineTest : public ::testing::Test {
public:
IntrusiveRefCntPtr<DiagnosticsEngine> Diags;
SmallVector<const char *, 32> GeneratedArgs;
SmallVector<std::string, 32> GeneratedArgsStorage;
CompilerInvocation CInvok;

const char *operator()(const Twine &Arg) {
return GeneratedArgsStorage.emplace_back(Arg.str()).c_str();
}

CC1CommandLineGenerationTest()
CommandLineTest()
: Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions())) {}
};

TEST_F(OptsPopulationTest, OptIsInitializedWithCustomDefaultValue) {
TEST_F(CommandLineTest, OptIsInitializedWithCustomDefaultValue) {
const char *Args[] = {"clang", "-xc++"};

CompilerInvocation::CreateFromArgs(CInvok, Args, *Diags);

ASSERT_TRUE(CInvok.getFrontendOpts().UseTemporary);
}

TEST_F(OptsPopulationTest, OptOfNegativeFlagIsPopulatedWithFalse) {
TEST_F(CommandLineTest, OptOfNegativeFlagIsPopulatedWithFalse) {
const char *Args[] = {"clang", "-xc++", "-fno-temp-file"};

CompilerInvocation::CreateFromArgs(CInvok, Args, *Diags);

ASSERT_FALSE(CInvok.getFrontendOpts().UseTemporary);
}

TEST_F(OptsPopulationTest, OptsOfImpliedPositiveFlagArePopulatedWithTrue) {
TEST_F(CommandLineTest, OptsOfImpliedPositiveFlagArePopulatedWithTrue) {
const char *Args[] = {"clang", "-xc++", "-cl-unsafe-math-optimizations"};

CompilerInvocation::CreateFromArgs(CInvok, Args, *Diags);
Expand All @@ -76,37 +69,33 @@ TEST_F(OptsPopulationTest, OptsOfImpliedPositiveFlagArePopulatedWithTrue) {
ASSERT_TRUE(CInvok.getLangOpts()->AllowRecip);
}

TEST_F(CC1CommandLineGenerationTest, CanGenerateCC1CommandLineFlag) {
TEST_F(CommandLineTest, CanGenerateCC1CommandLineFlag) {
const char *Args[] = {"clang", "-xc++", "-fmodules-strict-context-hash", "-"};

CompilerInvocation CInvok;
CompilerInvocation::CreateFromArgs(CInvok, Args, *Diags);

CInvok.generateCC1CommandLine(GeneratedArgs, *this);

ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fmodules-strict-context-hash")));
}

TEST_F(CC1CommandLineGenerationTest, CanGenerateCC1CommandLineSeparate) {
TEST_F(CommandLineTest, CanGenerateCC1CommandLineSeparate) {
const char *TripleCStr = "i686-apple-darwin9";
const char *Args[] = {"clang", "-xc++", "-triple", TripleCStr, "-"};

CompilerInvocation CInvok;
CompilerInvocation::CreateFromArgs(CInvok, Args, *Diags);

CInvok.generateCC1CommandLine(GeneratedArgs, *this);

ASSERT_THAT(GeneratedArgs, Contains(StrEq(TripleCStr)));
}

TEST_F(CC1CommandLineGenerationTest,
CanGenerateCC1CommandLineSeparateRequiredPresent) {
TEST_F(CommandLineTest, CanGenerateCC1CommandLineSeparateRequiredPresent) {
const std::string DefaultTriple =
llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple());
const char *Args[] = {"clang", "-xc++", "-triple", DefaultTriple.c_str(),
"-"};

CompilerInvocation CInvok;
CompilerInvocation::CreateFromArgs(CInvok, Args, *Diags);

CInvok.generateCC1CommandLine(GeneratedArgs, *this);
Expand All @@ -115,13 +104,11 @@ TEST_F(CC1CommandLineGenerationTest,
ASSERT_THAT(GeneratedArgs, Contains(StrEq(DefaultTriple.c_str())));
}

TEST_F(CC1CommandLineGenerationTest,
CanGenerateCC1CommandLineSeparateRequiredAbsent) {
TEST_F(CommandLineTest, CanGenerateCC1CommandLineSeparateRequiredAbsent) {
const std::string DefaultTriple =
llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple());
const char *Args[] = {"clang", "-xc++", "-"};

CompilerInvocation CInvok;
CompilerInvocation::CreateFromArgs(CInvok, Args, *Diags);

CInvok.generateCC1CommandLine(GeneratedArgs, *this);
Expand All @@ -130,12 +117,11 @@ TEST_F(CC1CommandLineGenerationTest,
ASSERT_THAT(GeneratedArgs, Contains(StrEq(DefaultTriple.c_str())));
}

TEST_F(CC1CommandLineGenerationTest, CanGenerateCC1CommandLineSeparateEnum) {
TEST_F(CommandLineTest, CanGenerateCC1CommandLineSeparateEnum) {
const char *RelocationModelCStr = "static";
const char *Args[] = {"clang", "-xc++", "-mrelocation-model",
RelocationModelCStr, "-"};

CompilerInvocation CInvok;
CompilerInvocation::CreateFromArgs(CInvok, Args, *Diags);

CInvok.generateCC1CommandLine(GeneratedArgs, *this);
Expand All @@ -154,32 +140,29 @@ TEST_F(CC1CommandLineGenerationTest, CanGenerateCC1CommandLineSeparateEnum) {
ASSERT_THAT(GeneratedArgs, Each(StrNe(RelocationModelCStr)));
}

TEST_F(CC1CommandLineGenerationTest, NotPresentNegativeFlagNotGenerated) {
TEST_F(CommandLineTest, NotPresentNegativeFlagNotGenerated) {
const char *Args[] = {"clang", "-xc++"};

CompilerInvocation CInvok;
CompilerInvocation::CreateFromArgs(CInvok, Args, *Diags);

CInvok.generateCC1CommandLine(GeneratedArgs, *this);

ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fno-temp-file"))));
}

TEST_F(CC1CommandLineGenerationTest, PresentNegativeFlagGenerated) {
TEST_F(CommandLineTest, PresentNegativeFlagGenerated) {
const char *Args[] = {"clang", "-xc++", "-fno-temp-file"};

CompilerInvocation CInvok;
CompilerInvocation::CreateFromArgs(CInvok, Args, *Diags);

CInvok.generateCC1CommandLine(GeneratedArgs, *this);

ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fno-temp-file")));
}

TEST_F(CC1CommandLineGenerationTest, NotPresentAndNotImpliedNotGenerated) {
TEST_F(CommandLineTest, NotPresentAndNotImpliedNotGenerated) {
const char *Args[] = {"clang", "-xc++"};

CompilerInvocation CInvok;
CompilerInvocation::CreateFromArgs(CInvok, Args, *Diags);

CInvok.generateCC1CommandLine(GeneratedArgs, *this);
Expand All @@ -191,10 +174,9 @@ TEST_F(CC1CommandLineGenerationTest, NotPresentAndNotImpliedNotGenerated) {
ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-menable-unsafe-fp-math"))));
}

TEST_F(CC1CommandLineGenerationTest, NotPresentAndImpliedNotGenerated) {
TEST_F(CommandLineTest, NotPresentAndImpliedNotGenerated) {
const char *Args[] = {"clang", "-xc++", "-cl-unsafe-math-optimizations"};

CompilerInvocation CInvok;
CompilerInvocation::CreateFromArgs(CInvok, Args, *Diags);

CInvok.generateCC1CommandLine(GeneratedArgs, *this);
Expand All @@ -205,11 +187,10 @@ TEST_F(CC1CommandLineGenerationTest, NotPresentAndImpliedNotGenerated) {
ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-menable-unsafe-fp-math"))));
}

TEST_F(CC1CommandLineGenerationTest, PresentAndImpliedNotGenerated) {
TEST_F(CommandLineTest, PresentAndImpliedNotGenerated) {
const char *Args[] = {"clang", "-xc++", "-cl-unsafe-math-optimizations",
"-cl-mad-enable", "-menable-unsafe-fp-math"};

CompilerInvocation CInvok;
CompilerInvocation::CreateFromArgs(CInvok, Args, *Diags);

CInvok.generateCC1CommandLine(GeneratedArgs, *this);
Expand All @@ -220,11 +201,10 @@ TEST_F(CC1CommandLineGenerationTest, PresentAndImpliedNotGenerated) {
ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-menable-unsafe-fp-math"))));
}

TEST_F(CC1CommandLineGenerationTest, PresentAndNotImpliedGenerated) {
TEST_F(CommandLineTest, PresentAndNotImpliedGenerated) {
const char *Args[] = {"clang", "-xc++", "-cl-mad-enable",
"-menable-unsafe-fp-math"};

CompilerInvocation CInvok;
CompilerInvocation::CreateFromArgs(CInvok, Args, *Diags);

CInvok.generateCC1CommandLine(GeneratedArgs, *this);
Expand Down

0 comments on commit cf2bb22

Please sign in to comment.