-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[clang][modules-driver] Add scanner to detect C++20 module presence #145220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
2574e28
499416f
465e613
d4cf1f2
71bdec5
0b25368
c2d6f40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -577,6 +577,19 @@ def err_drv_reduced_module_output_overrided : Warning< | |
| "please consider use '-fmodule-output=' to specify the output file for reduced BMI explicitly">, | ||
| InGroup<DiagGroup<"reduced-bmi-output-overrided">>; | ||
|
|
||
| def remark_fmodules_driver_enabled : Remark< | ||
| "support for explicit module builds enabled (experimental)">, | ||
| InGroup<ModulesDriver>; | ||
| def remark_found_cxx20_module_usage : Remark< | ||
| "found C++20 module usage in file '%0'">, | ||
| InGroup<ModulesDriver>; | ||
| def remark_performing_explicit_module_build : Remark< | ||
|
||
| "performing explicit module build">, | ||
| InGroup<ModulesDriver>; | ||
| def warn_modules_driver_unsupported_standard : Warning< | ||
| "'-fmodules-driver' is not supported before C++20">, | ||
| InGroup<ModulesDriver>; | ||
|
|
||
| def warn_drv_delayed_template_parsing_after_cxx20 : Warning< | ||
| "-fdelayed-template-parsing is deprecated after C++20">, | ||
| InGroup<DiagGroup<"delayed-template-parsing-in-cxx20">>; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -505,6 +505,8 @@ class Driver { | |
|
|
||
| /// BuildActions - Construct the list of actions to perform for the | ||
| /// given arguments, which are only done for a single architecture. | ||
| /// If the compilation is an explicit module build, delegates to | ||
| /// BuildExplicitModuleBuildActions. Otherwise, uses BuildDefaultActions. | ||
| /// | ||
| /// \param C - The compilation that is being built. | ||
| /// \param Args - The input arguments. | ||
|
|
@@ -790,6 +792,35 @@ class Driver { | |
| /// compilation based on which -f(no-)?lto(=.*)? option occurs last. | ||
| void setLTOMode(const llvm::opt::ArgList &Args); | ||
|
|
||
| /// BuildDefaultActions - Constructs the list of actions to perform | ||
| /// for the provided arguments, which are only done for a single architecture. | ||
| /// | ||
| /// \param C - The compilation that is being built. | ||
| /// \param Args - The input arguments. | ||
| /// \param Actions - The list to store the resulting actions onto. | ||
| void BuildDefaultActions(Compilation &C, llvm::opt::DerivedArgList &Args, | ||
| const InputList &Inputs, ActionList &Actions) const; | ||
|
|
||
| /// BuildExplicitModuleBuildActions - Performs a dependency scan and | ||
| /// constructs the list of actions to perform for dependency order and | ||
| /// the provided arguments. This is only done for a single a architecture. | ||
| /// | ||
| /// \param C - The compilation that is being built. | ||
| /// \param Args - The input arguments. | ||
| /// \param Actions - The list to store the resulting actions onto. | ||
| void BuildExplicitModuleBuildActions(Compilation &C, | ||
|
||
| llvm::opt::DerivedArgList &Args, | ||
| const InputList &Inputs, | ||
| ActionList &Actions) const; | ||
|
|
||
| /// Scans the leading lines of the C++ source inputs to detect C++20 module | ||
| /// usage. | ||
| /// | ||
| /// \returns True if module usage is detected, false otherwise, or an error on | ||
| /// read failure. | ||
| llvm::ErrorOr<bool> | ||
| ScanInputsForCXXModuleUsage(const InputList &Inputs) const; | ||
|
|
||
| /// Retrieves a ToolChain for a particular \p Target triple. | ||
| /// | ||
| /// Will cache ToolChains for the life of the driver object, and create them | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -53,6 +53,7 @@ | |||||
| #include "ToolChains/WebAssembly.h" | ||||||
| #include "ToolChains/XCore.h" | ||||||
| #include "ToolChains/ZOS.h" | ||||||
| #include "clang/Basic/CharInfo.h" | ||||||
| #include "clang/Basic/DiagnosticDriver.h" | ||||||
| #include "clang/Basic/TargetID.h" | ||||||
| #include "clang/Basic/Version.h" | ||||||
|
|
@@ -4285,6 +4286,13 @@ void Driver::handleArguments(Compilation &C, DerivedArgList &Args, | |||||
| YcArg = nullptr; | ||||||
| } | ||||||
|
|
||||||
| if (Args.hasArgNoClaim(options::OPT_fmodules_driver)) | ||||||
| // TODO: Check against all incompatible -fmodules-driver arguments | ||||||
| if (!ModulesModeCXX20) { | ||||||
| Diag(diag::warn_modules_driver_unsupported_standard); | ||||||
| Args.eraseArg(options::OPT_fmodules_driver); | ||||||
| } | ||||||
|
|
||||||
| Arg *FinalPhaseArg; | ||||||
| phases::ID FinalPhase = getFinalPhase(Args, &FinalPhaseArg); | ||||||
|
|
||||||
|
|
@@ -4403,6 +4411,174 @@ void Driver::handleArguments(Compilation &C, DerivedArgList &Args, | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| static void skipWhitespace(const char *&Ptr) { | ||||||
| while (isWhitespace(*Ptr)) | ||||||
| ++Ptr; | ||||||
| } | ||||||
|
|
||||||
| // Returns the length of EOL, either 0 (no end-of-line), 1 (\n) or 2 (\r\n). | ||||||
| static unsigned isEOL(const char *Ptr) { | ||||||
| if (*Ptr == '\0') | ||||||
| return 0; | ||||||
| if (*(Ptr + 1) != '\0' && isVerticalWhitespace(Ptr[0]) && | ||||||
| isVerticalWhitespace(Ptr[1]) && Ptr[0] != Ptr[1]) | ||||||
| return 2; | ||||||
| return !!isVerticalWhitespace(Ptr[0]); | ||||||
| } | ||||||
|
|
||||||
| static void skipLine(const char *&Ptr) { | ||||||
| for (;;) { | ||||||
| char LastNonWhitespace = ' '; | ||||||
| while (!isVerticalWhitespace(*Ptr) && *Ptr != '\0') { | ||||||
| if (!isHorizontalWhitespace(*Ptr)) | ||||||
| LastNonWhitespace = *Ptr; | ||||||
| ++Ptr; | ||||||
| } | ||||||
|
|
||||||
| const unsigned Len = isEOL(Ptr); | ||||||
| if (!Len) | ||||||
| return; | ||||||
|
|
||||||
| Ptr += Len; | ||||||
| if (LastNonWhitespace != '\\') | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Returns the length of a line splice sequence (including trailing | ||||||
| // whitespace), or 0 if no line splice is found. | ||||||
| static unsigned isLineSplice(const char *Start) { | ||||||
| if (*Start != '\\') | ||||||
| return 0; | ||||||
|
|
||||||
| const char *Ptr = Start + 1; | ||||||
| while (isHorizontalWhitespace(*Ptr)) | ||||||
| ++Ptr; | ||||||
|
|
||||||
| if (unsigned Len = isEOL(Ptr)) | ||||||
| return Ptr - Start + Len; | ||||||
| return 0; | ||||||
| } | ||||||
|
|
||||||
| static bool trySkipLineSplice(const char *&Ptr) { | ||||||
| if (unsigned Len = isLineSplice(Ptr); Len) { | ||||||
| Ptr += Len; | ||||||
| return true; | ||||||
| } | ||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| static bool trySkipDirective(const char *&Ptr) { | ||||||
| if (*Ptr != '#') | ||||||
| return false; | ||||||
|
|
||||||
| ++Ptr; | ||||||
| skipLine(Ptr); | ||||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| static bool trySkipLineComment(const char *&Ptr) { | ||||||
| if (Ptr[0] != '/' || Ptr[1] != '/') | ||||||
| return false; | ||||||
|
|
||||||
| Ptr += 2; | ||||||
| skipLine(Ptr); | ||||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| static bool trySkipBlockComment(const char *&Ptr) { | ||||||
| if (Ptr[0] != '/' || Ptr[1] != '*') | ||||||
| return false; | ||||||
|
|
||||||
| Ptr += 2; | ||||||
| while (*Ptr != '\0') { | ||||||
| if (Ptr[0] == '*' && Ptr[1] == '/') { | ||||||
| Ptr += 2; // '*/' | ||||||
| return true; | ||||||
| } | ||||||
| ++Ptr; | ||||||
| } | ||||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| static bool trySkipComment(const char *&Ptr) { | ||||||
| return trySkipLineComment(Ptr) || trySkipBlockComment(Ptr); | ||||||
| } | ||||||
|
|
||||||
| // Skipps over comments and (non-module) directives | ||||||
| static void skipToRelevantCXXModuleText(const char *&Ptr) { | ||||||
| while (*Ptr != '\0') { | ||||||
| skipWhitespace(Ptr); | ||||||
| if (trySkipComment(Ptr) || trySkipDirective(Ptr) || trySkipLineSplice(Ptr)) | ||||||
| continue; | ||||||
| break; // Found relevant text! | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| static bool scanBufferForCXXModuleUsage(const llvm::MemoryBuffer &Buffer) { | ||||||
| const char *Ptr = Buffer.getBufferStart(); | ||||||
| skipToRelevantCXXModuleText(Ptr); | ||||||
|
|
||||||
| // Check if buffer has enough bytes left to check for the module-related | ||||||
| // declaration fragment we want to check without making potentially | ||||||
|
||||||
| // declaration fragment we want to check without making potentially | |
| // declaration fragment we want to check without making the potentially |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to Driver:: qualify this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this specific remark is needed, it's just saying the flag is passed, which is simple enough to not need a remark for.