Skip to content

Commit

Permalink
[flang] Fix crash from empty -DMACRO= (bug #64837)
Browse files Browse the repository at this point in the history
Some vector indexing code in the preprocessor fails with empty
tokens or token sequences in predefined macros.

Fixes #64837.

Differential Revision: https://reviews.llvm.org/D158451
  • Loading branch information
klausler committed Aug 23, 2023
1 parent adaf545 commit d40e600
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
20 changes: 11 additions & 9 deletions flang/lib/Parser/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)};
Expand Down
10 changes: 8 additions & 2 deletions flang/lib/Parser/token-sequence.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions flang/test/Preprocessing/bug64837.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
! RUN: %flang -E -DMACRO= %s 2>&1 | FileCheck %s
!CHECK: integer, parameter :: p = +1
integer, parameter :: p = MACRO +1
end

0 comments on commit d40e600

Please sign in to comment.