[flang-commits] [PATCH] D158451: [flang] Fix crash from empty -DMACRO= (bug #64837)
Peter Klausler via Phabricator via flang-commits
flang-commits at lists.llvm.org
Wed Aug 23 10:21:02 PDT 2023
This revision was automatically updated to reflect the committed changes.
Closed by commit rGd40e6005db99: [flang] Fix crash from empty -DMACRO= (bug #64837) (authored by klausler).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D158451/new/
https://reviews.llvm.org/D158451
Files:
flang/lib/Parser/preprocessor.cpp
flang/lib/Parser/token-sequence.h
flang/test/Preprocessing/bug64837.F90
Index: flang/test/Preprocessing/bug64837.F90
===================================================================
--- /dev/null
+++ 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
Index: flang/lib/Parser/token-sequence.h
===================================================================
--- flang/lib/Parser/token-sequence.h
+++ flang/lib/Parser/token-sequence.h
@@ -61,11 +61,17 @@
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 {
Index: flang/lib/Parser/preprocessor.cpp
===================================================================
--- flang/lib/Parser/preprocessor.cpp
+++ flang/lib/Parser/preprocessor.cpp
@@ -296,16 +296,18 @@
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)};
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158451.552785.patch
Type: text/x-patch
Size: 2652 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230823/9a04b1da/attachment-0001.bin>
More information about the flang-commits
mailing list