From 9188e5887a9ed273dc3f12b00b2ef4e220370ec8 Mon Sep 17 00:00:00 2001 From: yangdian Date: Mon, 14 Aug 2023 19:50:00 +0800 Subject: [PATCH] [BSC] not expanding header files under rewrite bsc --- clang/include/clang/Driver/Options.td | 4 ++- clang/include/clang/Lex/PreprocessorOptions.h | 2 ++ clang/lib/Driver/ToolChains/Clang.cpp | 9 ++++--- clang/lib/Lex/PPDirectives.cpp | 26 +++++++++++++++++++ .../async-different-return-type2.cbs | 6 ++++- .../Driver/rewrite-bsc/rewrite_bsc_header.hbs | 2 ++ .../rewrite-bsc/rewrite_bsc_with_header.cbs | 5 +++- 7 files changed, 48 insertions(+), 6 deletions(-) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 2338a5f39105..ff25b3c2d7f5 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -6240,7 +6240,9 @@ def setup_static_analyzer : Flag<["-"], "setup-static-analyzer">, def disable_pragma_debug_crash : Flag<["-"], "disable-pragma-debug-crash">, HelpText<"Disable any #pragma clang __debug that can lead to crashing behavior. This is meant for testing.">, MarshallingInfoFlag>; - +def disable_expansion : Flag<["-"], "disable-expansion">, + HelpText<"Disable any non-hbs files expansions.">, + MarshallingInfoFlag>; } // let Flags = [CC1Option, NoDriverOption] //===----------------------------------------------------------------------===// diff --git a/clang/include/clang/Lex/PreprocessorOptions.h b/clang/include/clang/Lex/PreprocessorOptions.h index 4cf18e98f051..c74eb4a55acd 100644 --- a/clang/include/clang/Lex/PreprocessorOptions.h +++ b/clang/include/clang/Lex/PreprocessorOptions.h @@ -110,6 +110,8 @@ public: /// When true, a PCH with modules cache path different to the current /// compilation will not be rejected. bool AllowPCHWithDifferentModulesCachePath = false; + + bool DisableExpansion = false; /// Dump declarations that are deserialized from PCH, for testing. bool DumpDeserializedPCHDecls = false; diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 69b63800f573..25c0f21070e1 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -756,9 +756,12 @@ static void addDashXForInput(const ArgList &Args, const InputInfo &Input, CmdArgs.push_back("-x"); if (Args.hasArg(options::OPT_rewrite_objc)) CmdArgs.push_back(types::getTypeName(types::TY_PP_ObjCXX)); - else if (Args.hasArg(options::OPT_rewrite_bsc)) + else if (Args.hasArg(options::OPT_rewrite_bsc)) { CmdArgs.push_back(types::getTypeName(types::TY_PP_BSC)); - else { + if (Input.getType() == types::TY_BSC) { + CmdArgs.push_back("-disable-expansion"); + } + } else { // Map the driver type to the frontend type. This is mostly an identity // mapping, except that the distinction between module interface units // and other source files does not exist at the frontend layer. @@ -5674,7 +5677,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, // preprocessor. // // FIXME: Support -fpreprocessed - if (types::getPreprocessedType(InputType) != types::TY_INVALID) + if (types::getPreprocessedType(InputType) != types::TY_INVALID || Args.hasArg(options::OPT_rewrite_bsc)) AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs); // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp index 9a8fd4391b41..17a596a3aacc 100644 --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -2524,6 +2524,32 @@ Preprocessor::ImportAction Preprocessor::HandleHeaderIncludeOrImport( return ImportAction::Failure; } + // For rewrite-bsc mode, we do not enter new file and re-lex the include directive. + if (getPreprocessorOpts().DisableExpansion && Filename.find(".hbs") == llvm::StringLiteral::npos) { + auto Toks = std::make_unique(4); + // Return the # and the token after it. + Token HashToken; + HashToken.startToken(); + HashToken.setKind(tok::hash); + HashToken.setLocation(IncludeTok.getLocation().getLocWithOffset(-1)); + HashToken.setLength(1); + Toks[0] = HashToken; + Toks[1] = IncludeTok; + Toks[2] = FilenameTok; + Token EndTok; + EndTok.startToken(); + EndTok.setKind(tok::eod); + EndTok.setLocation(FilenameTok.getEndLoc()); + EndTok.setLength(1); + Toks[3] = EndTok; + + // Enter this token stream so that we re-lex the tokens. Make sure to + // enable macro expansion, in case the token after the # is an identifier + // that is expanded. + EnterTokenStream(std::move(Toks), 4, false, /*IsReinject*/false); + return {ImportAction::None}; + } + // If all is good, enter the new file! if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation(), IsFirstIncludeOfFile)) diff --git a/clang/test/BSC/Coroutine/Other/DifferenctReturnType/async-different-return-type2.cbs b/clang/test/BSC/Coroutine/Other/DifferenctReturnType/async-different-return-type2.cbs index 456c080f86bc..f0bad9e07048 100644 --- a/clang/test/BSC/Coroutine/Other/DifferenctReturnType/async-different-return-type2.cbs +++ b/clang/test/BSC/Coroutine/Other/DifferenctReturnType/async-different-return-type2.cbs @@ -1,6 +1,7 @@ // RUN: %clang %s -o %test.output // RUN: %test.output // RUN: %clang -rewrite-bsc %s -o %t-rw.c +// RUN: FileCheck --input-file=%t-rw.c %s // RUN: %clang %t-rw.c -o %t-rw.output // RUN: %t-rw.output @@ -25,4 +26,7 @@ int main() { this.vtable->poll(this.data); this.vtable->free(this.data); return 0; -} \ No newline at end of file +} + +// CHECK: #include "stdlib.h" +// CHECK: #include \ No newline at end of file diff --git a/clang/test/BSC/Driver/rewrite-bsc/rewrite_bsc_header.hbs b/clang/test/BSC/Driver/rewrite-bsc/rewrite_bsc_header.hbs index d84c3654a44e..5c8d4fec8d37 100644 --- a/clang/test/BSC/Driver/rewrite-bsc/rewrite_bsc_header.hbs +++ b/clang/test/BSC/Driver/rewrite-bsc/rewrite_bsc_header.hbs @@ -1,6 +1,8 @@ #ifndef REWRITE_H #define REWRITE_H +#include + struct Foo { int a; }; diff --git a/clang/test/BSC/Driver/rewrite-bsc/rewrite_bsc_with_header.cbs b/clang/test/BSC/Driver/rewrite-bsc/rewrite_bsc_with_header.cbs index db1e8810f972..96ec1db51b1e 100644 --- a/clang/test/BSC/Driver/rewrite-bsc/rewrite_bsc_with_header.cbs +++ b/clang/test/BSC/Driver/rewrite-bsc/rewrite_bsc_with_header.cbs @@ -3,6 +3,7 @@ // RUN: %clang %t-rw.c -o %t-rw.output // RUN: %t-rw.output +#include #include "rewrite_bsc_header.hbs" int main() { @@ -14,7 +15,9 @@ int main() { return 0; } -// CHECK: struct Foo { +// CHECK: #include +// CHECK-NEXT: #include +// CHECK-NEXT: struct Foo { // CHECK-NEXT: int a; // CHECK-NEXT: }; -- Gitee