aboutsummaryrefslogtreecommitdiff
path: root/flang/lib/Parser/preprocessor.cpp
diff options
context:
space:
mode:
authorpeter klausler <pklausler@nvidia.com>2021-02-10 16:15:20 -0800
committerpeter klausler <pklausler@nvidia.com>2021-02-11 11:25:41 -0800
commit8880a63a15a011b3265a184c398f2ff81570cdb6 (patch)
tree9b7f6490bf13cca408cea0f0f3ae7826c3543cd0 /flang/lib/Parser/preprocessor.cpp
parent2c7077e67dc77834607c877ad6e8caa6e33ae238 (diff)
downloadllvm-8880a63a15a011b3265a184c398f2ff81570cdb6.zip
llvm-8880a63a15a011b3265a184c398f2ff81570cdb6.tar.gz
llvm-8880a63a15a011b3265a184c398f2ff81570cdb6.tar.bz2
[flang] Don't perform macro replacement unless *.F, *.F90, &c.
Avoid spurious and confusing macro replacements from things like -DPIC on Fortran source files whose suffixes indicate that preprocessing is not expected. Add gfortran-like "-cpp" and "-nocpp" flags to f18 to force predefinition of macros independent of the source file suffix. Differential Revision: https://reviews.llvm.org/D96464
Diffstat (limited to 'flang/lib/Parser/preprocessor.cpp')
-rw-r--r--flang/lib/Parser/preprocessor.cpp19
1 files changed, 10 insertions, 9 deletions
diff --git a/flang/lib/Parser/preprocessor.cpp b/flang/lib/Parser/preprocessor.cpp
index d3dd50a..a2eb77a 100644
--- a/flang/lib/Parser/preprocessor.cpp
+++ b/flang/lib/Parser/preprocessor.cpp
@@ -232,20 +232,18 @@ static std::string FormatTime(const std::time_t &now, const char *format) {
std::strftime(buffer, sizeof buffer, format, std::localtime(&now))};
}
-Preprocessor::Preprocessor(AllSources &allSources) : allSources_{allSources} {
+Preprocessor::Preprocessor(AllSources &allSources) : allSources_{allSources} {}
+
+void Preprocessor::DefineStandardMacros() {
// Capture current local date & time once now to avoid having the values
// of __DATE__ or __TIME__ change during compilation.
std::time_t now;
std::time(&now);
- definitions_.emplace(SaveTokenAsName("__DATE__"s), // e.g., "Jun 16 1904"
- Definition{FormatTime(now, "\"%h %e %Y\""), allSources});
- definitions_.emplace(SaveTokenAsName("__TIME__"s), // e.g., "23:59:60"
- Definition{FormatTime(now, "\"%T\""), allSources});
+ Define("__DATE__"s, FormatTime(now, "\"%h %e %Y\"")); // e.g., "Jun 16 1904"
+ Define("__TIME__"s, FormatTime(now, "\"%T\"")); // e.g., "23:59:60"
// The values of these predefined macros depend on their invocation sites.
- definitions_.emplace(
- SaveTokenAsName("__FILE__"s), Definition{"__FILE__"s, allSources});
- definitions_.emplace(
- SaveTokenAsName("__LINE__"s), Definition{"__LINE__"s, allSources});
+ Define("__FILE__"s, "__FILE__"s);
+ Define("__LINE__"s, "__LINE__"s);
}
void Preprocessor::Define(std::string macro, std::string value) {
@@ -257,6 +255,9 @@ void Preprocessor::Undefine(std::string macro) { definitions_.erase(macro); }
std::optional<TokenSequence> Preprocessor::MacroReplacement(
const TokenSequence &input, Prescanner &prescanner) {
// Do quick scan for any use of a defined name.
+ if (definitions_.empty()) {
+ return std::nullopt;
+ }
std::size_t tokens{input.SizeInTokens()};
std::size_t j;
for (j = 0; j < tokens; ++j) {