[flang-commits] [flang] d40e600 - [flang] Fix crash from empty -DMACRO= (bug #64837)
Peter Klausler via flang-commits
flang-commits at lists.llvm.org
Wed Aug 23 10:20:59 PDT 2023
Author: Peter Klausler
Date: 2023-08-23T10:20:45-07:00
New Revision: d40e6005db992007d0d856f07eb6c5770a45389b
URL: https://github.com/llvm/llvm-project/commit/d40e6005db992007d0d856f07eb6c5770a45389b
DIFF: https://github.com/llvm/llvm-project/commit/d40e6005db992007d0d856f07eb6c5770a45389b.diff
LOG: [flang] Fix crash from empty -DMACRO= (bug #64837)
Some vector indexing code in the preprocessor fails with empty
tokens or token sequences in predefined macros.
Fixes https://github.com/llvm/llvm-project/issues/64837.
Differential Revision: https://reviews.llvm.org/D158451
Added:
flang/test/Preprocessing/bug64837.F90
Modified:
flang/lib/Parser/preprocessor.cpp
flang/lib/Parser/token-sequence.h
Removed:
################################################################################
diff --git a/flang/lib/Parser/preprocessor.cpp b/flang/lib/Parser/preprocessor.cpp
index c78a9e1f4d9f48..5af16323d0129b 100644
--- a/flang/lib/Parser/preprocessor.cpp
+++ b/flang/lib/Parser/preprocessor.cpp
@@ -296,16 +296,18 @@ std::optional<TokenSequence> Preprocessor::MacroReplacement(
if (!def->isFunctionLike()) {
bool isRenaming{false};
if (def->isPredefined()) {
- std::string name{def->replacement().TokenAt(0).ToString()};
std::string repl;
- if (name == "__FILE__") {
- repl = "\""s +
- allSources_.GetPath(prescanner.GetCurrentProvenance()) + '"';
- } else if (name == "__LINE__") {
- std::string buf;
- llvm::raw_string_ostream ss{buf};
- ss << allSources_.GetLineNumber(prescanner.GetCurrentProvenance());
- repl = ss.str();
+ if (!def->replacement().empty()) {
+ std::string name{def->replacement().TokenAt(0).ToString()};
+ if (name == "__FILE__") {
+ repl = "\""s +
+ allSources_.GetPath(prescanner.GetCurrentProvenance()) + '"';
+ } else if (name == "__LINE__") {
+ std::string buf;
+ llvm::raw_string_ostream ss{buf};
+ ss << allSources_.GetLineNumber(prescanner.GetCurrentProvenance());
+ repl = ss.str();
+ }
}
if (!repl.empty()) {
ProvenanceRange insert{allSources_.AddCompilerInsertion(repl)};
diff --git a/flang/lib/Parser/token-sequence.h b/flang/lib/Parser/token-sequence.h
index ab506da82dde6f..6b9e1f87ee0160 100644
--- a/flang/lib/Parser/token-sequence.h
+++ b/flang/lib/Parser/token-sequence.h
@@ -61,11 +61,17 @@ class TokenSequence {
std::size_t SizeInTokens() const { return start_.size(); }
std::size_t SizeInChars() const { return char_.size(); }
- CharBlock ToCharBlock() const { return {&char_[0], char_.size()}; }
+ CharBlock ToCharBlock() const {
+ return char_.empty() ? CharBlock{} : CharBlock{&char_[0], char_.size()};
+ }
std::string ToString() const { return ToCharBlock().ToString(); }
CharBlock TokenAt(std::size_t token) const {
- return {&char_[start_.at(token)], TokenBytes(token)};
+ if (auto bytes{TokenBytes(token)}) {
+ return {&char_[start_.at(token)], bytes};
+ } else { // char_ could be empty
+ return {};
+ }
}
char CharAt(std::size_t j) const { return char_.at(j); }
CharBlock CurrentOpenToken() const {
diff --git a/flang/test/Preprocessing/bug64837.F90 b/flang/test/Preprocessing/bug64837.F90
new file mode 100644
index 00000000000000..dadc830f5590fb
--- /dev/null
+++ b/flang/test/Preprocessing/bug64837.F90
@@ -0,0 +1,4 @@
+! RUN: %flang -E -DMACRO= %s 2>&1 | FileCheck %s
+!CHECK: integer, parameter :: p = +1
+integer, parameter :: p = MACRO +1
+end
More information about the flang-commits
mailing list