diff --git a/clang/unittests/Frontend/CompilerInvocationTest.cpp b/clang/unittests/Frontend/CompilerInvocationTest.cpp --- a/clang/unittests/Frontend/CompilerInvocationTest.cpp +++ b/clang/unittests/Frontend/CompilerInvocationTest.cpp @@ -77,6 +77,153 @@ ASSERT_TRUE(Invocation.getFrontendOpts().UseTemporary); } +// Boolean option with a keypath that defaults to true. +// The flag with negative spelling can set the keypath to false. +// The flag with positive spelling can reset the keypath to true. + +TEST_F(CommandLineTest, BoolOptionDefaultTruePresentNone) { + const char *Args[] = {""}; + + CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags); + ASSERT_FALSE(Diags->hasErrorOccurred()); + ASSERT_TRUE(Invocation.getCodeGenOpts().Autolink); + + // TODO: Test argument generation. +} + +TEST_F(CommandLineTest, BoolOptionDefaultTruePresentNegChange) { + const char *Args[] = {"-fno-autolink"}; + + CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags); + ASSERT_FALSE(Diags->hasErrorOccurred()); + ASSERT_FALSE(Invocation.getCodeGenOpts().Autolink); + + // TODO: Test argument generation. +} + +TEST_F(CommandLineTest, BoolOptionDefaultTruePresentPosReset) { + const char *Args[] = {"-fautolink"}; + + CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags); + ASSERT_TRUE(Diags->hasErrorOccurred()); // Driver-only flag. + ASSERT_TRUE(Invocation.getCodeGenOpts().Autolink); +} + +// Boolean option with a keypath that defaults to false. +// The flag with negative spelling can set the keypath to true. +// The flag with positive spelling can reset the keypath to false. + +TEST_F(CommandLineTest, BoolOptionDefaultFalsePresentNone) { + const char *Args[] = {""}; + + CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags); + ASSERT_FALSE(Diags->hasErrorOccurred()); + ASSERT_FALSE(Invocation.getCodeGenOpts().NoInlineLineTables); + + // TODO: Test argument generation. +} + +TEST_F(CommandLineTest, BoolOptionDefaultFalsePresentNegChange) { + const char *Args[] = {"-gno-inline-line-tables"}; + + CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags); + ASSERT_FALSE(Diags->hasErrorOccurred()); + ASSERT_TRUE(Invocation.getCodeGenOpts().NoInlineLineTables); + + // TODO: Test argument generation. +} + +TEST_F(CommandLineTest, BoolOptionDefaultFalsePresentPosReset) { + const char *Args[] = {"-ginline-line-tables"}; + + CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags); + ASSERT_TRUE(Diags->hasErrorOccurred()); // Driver-only flag. + ASSERT_FALSE(Invocation.getCodeGenOpts().NoInlineLineTables); +} + +// Boolean option with a keypath that defaults to false. +// The flag with positive spelling can set the keypath to true. +// The flag with negative spelling can reset the keypath to false. + +TEST_F(CommandLineTest, BoolOptionDefaultFalsePresentNoneX) { + const char *Args[] = {""}; + + CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags); + ASSERT_FALSE(Diags->hasErrorOccurred()); + ASSERT_FALSE(Invocation.getCodeGenOpts().CodeViewGHash); + + // TODO: Test argument generation. +} + +TEST_F(CommandLineTest, BoolOptionDefaultFalsePresentPosChange) { + const char *Args[] = {"-gcodeview-ghash"}; + + CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags); + ASSERT_FALSE(Diags->hasErrorOccurred()); + ASSERT_TRUE(Invocation.getCodeGenOpts().CodeViewGHash); + + // TODO: Test argument generation. +} + +TEST_F(CommandLineTest, BoolOptionDefaultFalsePresentNegReset) { + const char *Args[] = {"-gno-codeview-ghash"}; + + CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags); + ASSERT_TRUE(Diags->hasErrorOccurred()); // Driver-only flag. + ASSERT_FALSE(Invocation.getCodeGenOpts().CodeViewGHash); +} + +// Boolean option with a keypath that defaults to an arbitrary expression. +// The flag with positive spelling can set the keypath to true. +// The flag with negative spelling can set the keypath to false. + +TEST_F(CommandLineTest, BoolOptionDefaultArbitraryTwoFlagsPresentNone) { + const char *Args = {""}; + + CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags); + + ASSERT_FALSE(Diags->hasErrorOccurred()); + ASSERT_EQ(Invocation.getCodeGenOpts().LegacyPassManager, + !static_cast(LLVM_ENABLE_NEW_PASS_MANAGER)); + + Invocation.generateCC1CommandLine(GeneratedArgs, *this); + + const char *ResetByFlag = LLVM_ENABLE_NEW_PASS_MANAGER + ? "-fno-legacy-pass-manager" + : "-flegacy-pass-manager"; + + const char *ChangedByFlag = LLVM_ENABLE_NEW_PASS_MANAGER + ? "-flegacy-pass-manager" + : "-fno-legacy-pass-manager"; + + ASSERT_THAT(GeneratedArgs, Contains(StrEq(ResetByFlag))); + ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq(ChangedByFlag)))); +} + +TEST_F(CommandLineTest, BoolOptionDefaultArbitraryTwoFlagsPresentPos) { + const char *Args[] = {"-flegacy-pass-manager"}; + + CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags); + ASSERT_FALSE(Diags->hasErrorOccurred()); + ASSERT_TRUE(Invocation.getCodeGenOpts().LegacyPassManager); + + Invocation.generateCC1CommandLine(GeneratedArgs, *this); + ASSERT_THAT(GeneratedArgs, Contains(StrEq("-flegacy-pass-manager"))); + ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fno-legacy-pass-manager")))); +} + +TEST_F(CommandLineTest, BoolOptionDefaultArbitraryTwoFlagsPresentNeg) { + const char *Args[] = {"-fno-legacy-pass-manager"}; + + CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags); + ASSERT_FALSE(Diags->hasErrorOccurred()); + ASSERT_FALSE(Invocation.getCodeGenOpts().LegacyPassManager); + + Invocation.generateCC1CommandLine(GeneratedArgs, *this); + ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fno-legacy-pass-manager"))); + ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-flegacy-pass-manager")))); +} + TEST_F(CommandLineTest, CanGenerateCC1CommandLineFlag) { const char *Args[] = {"-fmodules-strict-context-hash"};