Skip to content

Commit 84394d5

Browse files
committed
[clang][modules] Fix CodeGen options that can affect the AST. (llvm#78816)
`OptimizationLevel` and `OptimizeSize` can affect the generated AST. They indirectly affect the `Optimize` and `OptimizeSize` frontend options, which in turn set predefined macro definitions. This fixes rdar://121228252. (cherry picked from commit c5a33be)
1 parent e49a4e3 commit 84394d5

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
// that have enumeration type and VALUE_CODEGENOPT is a code
1313
// generation option that describes a value rather than a flag.
1414
//
15+
// AFFECTING_VALUE_CODEGENOPT is used for code generation options that can
16+
// affect the AST.
17+
//
1518
//===----------------------------------------------------------------------===//
1619
#ifndef CODEGENOPT
1720
# error Define the CODEGENOPT macro to handle language options
@@ -27,6 +30,11 @@ CODEGENOPT(Name, Bits, Default)
2730
CODEGENOPT(Name, Bits, Default)
2831
#endif
2932

33+
#ifndef AFFECTING_VALUE_CODEGENOPT
34+
# define AFFECTING_VALUE_CODEGENOPT(Name, Bits, Default) \
35+
VALUE_CODEGENOPT(Name, Bits, Default)
36+
#endif
37+
3038
CODEGENOPT(DisableIntegratedAS, 1, 0) ///< -no-integrated-as
3139
CODEGENOPT(RelaxELFRelocations, 1, 1) ///< -Wa,-mrelax-relocations={yes,no}
3240
CODEGENOPT(AsmVerbose , 1, 0) ///< -dA, -fverbose-asm.
@@ -192,8 +200,10 @@ ENUM_CODEGENOPT(ObjCDispatchMethod, ObjCDispatchMethodKind, 2, Legacy)
192200
CODEGENOPT(ObjCConvertMessagesToRuntimeCalls , 1, 1)
193201
CODEGENOPT(ObjCAvoidHeapifyLocalBlocks, 1, 0)
194202

195-
VALUE_CODEGENOPT(OptimizationLevel, 2, 0) ///< The -O[0-3] option specified.
196-
VALUE_CODEGENOPT(OptimizeSize, 2, 0) ///< If -Os (==1) or -Oz (==2) is specified.
203+
204+
// The optimization options affect frontend options, whicn in turn do affect the AST.
205+
AFFECTING_VALUE_CODEGENOPT(OptimizationLevel, 2, 0) ///< The -O[0-3] option specified.
206+
AFFECTING_VALUE_CODEGENOPT(OptimizeSize, 2, 0) ///< If -Os (==1) or -Oz (==2) is specified.
197207

198208
CODEGENOPT(AtomicProfileUpdate , 1, 0) ///< Set -fprofile-update=atomic
199209
/// Choose profile instrumenation kind or no instrumentation.
@@ -445,3 +455,4 @@ CODEGENOPT(CtorDtorReturnThis, 1, 0)
445455
#undef CODEGENOPT
446456
#undef ENUM_CODEGENOPT
447457
#undef VALUE_CODEGENOPT
458+
#undef AFFECTING_VALUE_CODEGENOPT

clang/lib/Basic/CodeGenOptions.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ void CodeGenOptions::resetNonModularOptions(StringRef ModuleFormat) {
2727
#define ENUM_DEBUGOPT(Name, Type, Bits, Default)
2828
#define CODEGENOPT(Name, Bits, Default) Name = Default;
2929
#define ENUM_CODEGENOPT(Name, Type, Bits, Default) set##Name(Default);
30+
// Do not reset AST affecting code generation options.
31+
#define AFFECTING_VALUE_CODEGENOPT(Name, Bits, Default)
3032
#include "clang/Basic/CodeGenOptions.def"
3133

3234
// Next reset all debug options that can always be reset, because they never

clang/test/ClangScanDeps/strip-codegen-args.m

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
// RUN: clang-scan-deps -compilation-database %t/cdb1.json -format experimental-full > %t/result1.txt
66
// RUN: FileCheck %s -input-file %t/result1.txt
77

8-
// This tests that codegen option that do not affect the AST or generation of a module are removed.
8+
// This tests that codegen option that do not affect the AST or generation of a
9+
// module are removed. It also tests that the optimization options that affect
10+
// the AST are not reset to -O0.
911

1012
// CHECK: "modules": [
1113
// CHECK-NEXT: {
1214
// CHECK: "command-line": [
15+
// CHECK-NOT: "-O0"
1316
// CHECK-NOT: "-flto"
1417
// CHECK-NOT: "-fno-autolink"
1518
// CHECK-NOT: "-mrelax-relocations=no"
@@ -23,17 +26,17 @@
2326
[
2427
{
2528
"directory": "DIR",
26-
"command": "clang -Imodules/A -fmodules -fmodules-cache-path=DIR/module-cache -fimplicit-modules -flto -fno-autolink -Xclang -mrelax-relocations=no -fsyntax-only DIR/t1.m",
29+
"command": "clang -Imodules/A -fmodules -fmodules-cache-path=DIR/module-cache -fimplicit-modules -O2 -flto -fno-autolink -Xclang -mrelax-relocations=no -fsyntax-only DIR/t1.m",
2730
"file": "DIR/t1.m"
2831
},
2932
{
3033
"directory": "DIR",
31-
"command": "clang -Imodules/A -fmodules -fmodules-cache-path=DIR/module-cache -fimplicit-modules -flto=thin -fautolink -fsyntax-only DIR/t2.m",
34+
"command": "clang -Imodules/A -fmodules -fmodules-cache-path=DIR/module-cache -fimplicit-modules -O2 -flto=thin -fautolink -fsyntax-only DIR/t2.m",
3235
"file": "DIR/t2.m"
3336
},
3437
{
3538
"directory": "DIR",
36-
"command": "clang -Imodules/A -fmodules -fmodules-cache-path=DIR/module-cache -fimplicit-modules -flto=full -fsyntax-only DIR/t3.m",
39+
"command": "clang -Imodules/A -fmodules -fmodules-cache-path=DIR/module-cache -fimplicit-modules -O2 -flto=full -fsyntax-only DIR/t3.m",
3740
"file": "DIR/t2.m"
3841
}
3942
]

0 commit comments

Comments
 (0)