[clang] 9dc9b21 - [C++20][Modules][Driver][HU 4/N] Add fdirectives-only mode for preprocessing output.
Iain Sandoe via cfe-commits
cfe-commits at lists.llvm.org
Sat Apr 23 06:43:42 PDT 2022
Author: Iain Sandoe
Date: 2022-04-23T14:43:07+01:00
New Revision: 9dc9b21488ee2bbf54e20807585a74a17a33fceb
URL: https://github.com/llvm/llvm-project/commit/9dc9b21488ee2bbf54e20807585a74a17a33fceb
DIFF: https://github.com/llvm/llvm-project/commit/9dc9b21488ee2bbf54e20807585a74a17a33fceb.diff
LOG: [C++20][Modules][Driver][HU 4/N] Add fdirectives-only mode for preprocessing output.
When the -fdirectives-only option is used together with -E, the preprocessor
output reflects evaluation of if/then/else directives.
Thus it preserves macros that are still live after such processing.
This output can be consumed by a second compilation to produce a header unit.
We automatically invoke this (with -E) when we know that the job produces a
header unit so that the preprocessed output reflects the macros that will be
defined when the binary HU is emitted.
Differential Revision: https://reviews.llvm.org/D121591
Added:
clang/test/Driver/cxx20-fdirectives-only.cpp
Modified:
clang/lib/Driver/Driver.cpp
clang/lib/Driver/ToolChains/Clang.cpp
Removed:
################################################################################
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index b453e24769571..2bb6680cf5961 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -4303,10 +4303,14 @@ Action *Driver::ConstructPhaseAction(
OutputTy = types::TY_Dependencies;
} else {
OutputTy = Input->getType();
+ // For these cases, the preprocessor is only translating forms, the Output
+ // still needs preprocessing.
if (!Args.hasFlag(options::OPT_frewrite_includes,
options::OPT_fno_rewrite_includes, false) &&
!Args.hasFlag(options::OPT_frewrite_imports,
options::OPT_fno_rewrite_imports, false) &&
+ !Args.hasFlag(options::OPT_fdirectives_only,
+ options::OPT_fno_directives_only, false) &&
!CCGenDiagnostics)
OutputTy = types::getPreprocessedType(OutputTy);
assert(OutputTy != types::TY_INVALID &&
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index ddc9559f0d950..0fd639da45ddf 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -4594,6 +4594,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
if (Args.hasArg(options::OPT_rewrite_objc) &&
!Args.hasArg(options::OPT_g_Group))
CmdArgs.push_back("-P");
+ else if (JA.getType() == types::TY_PP_CXXHeaderUnit)
+ CmdArgs.push_back("-fdirectives-only");
}
} else if (isa<AssembleJobAction>(JA)) {
CmdArgs.push_back("-emit-obj");
@@ -6707,6 +6709,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
if (RewriteImports)
CmdArgs.push_back("-frewrite-imports");
+ if (Args.hasFlag(options::OPT_fdirectives_only,
+ options::OPT_fno_directives_only, false))
+ CmdArgs.push_back("-fdirectives-only");
+
// Enable rewrite includes if the user's asked for it or if we're generating
// diagnostics.
// TODO: Once -module-dependency-dir works with -frewrite-includes it'd be
diff --git a/clang/test/Driver/cxx20-fdirectives-only.cpp b/clang/test/Driver/cxx20-fdirectives-only.cpp
new file mode 100644
index 0000000000000..96dc4fdaa07ac
--- /dev/null
+++ b/clang/test/Driver/cxx20-fdirectives-only.cpp
@@ -0,0 +1,20 @@
+// Test -fdirectives-only cases.
+
+// We can manullay specify fdirectives-only, for any pre-processor job.
+// RUN: %clang -### -std=c++20 -E -fdirectives-only foo.hh 2>&1 | \
+// RUN: FileCheck -check-prefix=CHECK-NON-HU %s
+
+// Check that we automatically append -fdirectives-only for header-unit
+// preprocessor jobs.
+// RUN: %clang -### -std=c++20 -E -fmodule-header=user foo.hh 2>&1 | \
+// RUN: FileCheck -check-prefix=CHECK-HU %s
+
+// CHECK-NON-HU: "-E"
+// CHECK-NON-HU-SAME: "-fdirectives-only"
+// CHECK-NON-HU-SAME: "-o" "-"
+// CHECK-NON-HU-SAME: "-x" "c++-header" "foo.hh"
+
+// CHECK-HU: "-E"
+// CHECK-HU-SAME: "-fdirectives-only"
+// CHECK-HU-SAME: "-o" "-"
+// CHECK-HU-SAME: "-x" "c++-user-header" "foo.hh"
More information about the cfe-commits
mailing list