Skip to content
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

Use $CHPL_HOME in locations printed from frontend errors #22757

Merged
merged 2 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions compiler/parser/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -885,8 +885,8 @@ static bool shouldPrintHeaderForDecl(chpl::ID declId) {
return ret;
}

// Print out 'in function/module/initializer' etc...
static void maybePrintErrorHeader(chpl::ID id) {
// Print out 'In function/module/initializer' etc...
static void maybePrintErrorHeader(chpl::Context* context, chpl::ID id) {

// No ID associated with this error, so no UAST information.
if (id.isEmpty()) return;
Expand All @@ -901,7 +901,7 @@ static void maybePrintErrorHeader(chpl::ID id) {

auto& declLoc = chpl::parsing::locateId(gContext, declId);
auto line = declLoc.firstLine();
auto path = declLoc.path();
auto path = context->adjustPathForErrorMsg(declLoc.path());

fprintf(stderr, "%s:%d: In %s:\n", path.c_str(), line, declLabelStr);

Expand Down Expand Up @@ -929,7 +929,7 @@ static void dynoDisplayError(chpl::Context* context,
fprintf(stderr, "\n");
}
} else {
maybePrintErrorHeader(id);
maybePrintErrorHeader(context, id);

switch (err.kind()) {
case chpl::ErrorMessage::NOTE:
Expand Down
4 changes: 4 additions & 0 deletions frontend/include/chpl/framework/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,10 @@ class Context {
error/warning output (vs brief output). */
void setDetailedErrorOutput(bool useDetailed);

/** Normalize a path for output in an error message by replacing
any prefix with the value of CHPL_HOME with the string $CHPL_HOME */
UniqueString adjustPathForErrorMsg(UniqueString path);

/**
Run printchplenv, or return a cached result of doing so. To get output,
CHPL_HOME must have been provided via the constructor; otherwise, the
Expand Down
11 changes: 11 additions & 0 deletions frontend/lib/framework/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,17 @@ void Context::setDetailedErrorOutput(bool detailedErrors) {
this->detailedErrors = detailedErrors;
}

UniqueString Context::adjustPathForErrorMsg(UniqueString path) {
const std::string& chpl_home = this->chplHome();
size_t chpl_home_len = chpl_home.length();
if (chpl_home_len > 0 && path.startsWith(chpl_home)) {
// replace a prefix of the value of CHPL_HOME with $CHPL_HOME
return UniqueString::getConcat(this, "$CHPL_HOME",
path.c_str()+chpl_home_len);
}
return path;
}

llvm::ErrorOr<const ChplEnvMap&> Context::getChplEnv() {
if (config_.chplHome.empty() || computedChplEnv) return chplEnv;
auto chplEnvResult = ::chpl::getChplEnv(config_.chplEnvOverrides,
Expand Down
11 changes: 7 additions & 4 deletions frontend/lib/framework/ErrorWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,11 @@ static TermColorName kindColor(ErrorBase::Kind kind) {
return CLEAR;
}

static void writeFile(std::ostream& oss, const Location& loc) {
auto pathUstr = loc.path();
static void writeFile(Context* context,
std::ostream& oss,
const Location& loc) {
UniqueString pathUstr = loc.path();
if (context) pathUstr = context->adjustPathForErrorMsg(pathUstr);
auto path = pathUstr.c_str();
int lineno = loc.line();
bool validPath = (path != nullptr && path[0] != '\0');
Expand All @@ -126,7 +129,7 @@ void ErrorWriter::writeHeading(ErrorBase::Kind kind, ErrorType type,
oss_ << kindText(kind);
setColor(CLEAR);
oss_ << " in ";
writeFile(oss_, errordetail::locate(context, loc));
writeFile(context, oss_, errordetail::locate(context, loc));
if (outputFormat_ == DETAILED) {
// Second part of the error decoration
const char* name = ErrorBase::getTypeName(type);
Expand All @@ -148,7 +151,7 @@ void ErrorWriter::writeNote(IdOrLocation loc, const std::string& str) {
if (outputFormat_ == BRIEF) {
// Indent notes in brief mode to make things easier to organize
oss_ << " note in ";
writeFile(oss_, errordetail::locate(context, loc));
writeFile(context, oss_, errordetail::locate(context, loc));
oss_ << ": ";
} else {
// In detailed mode, the body is indented.
Expand Down
3 changes: 0 additions & 3 deletions test/unstable-keyword/in-function.bad

This file was deleted.

2 changes: 0 additions & 2 deletions test/unstable-keyword/in-function.future

This file was deleted.