Skip to content

Commit

Permalink
Fix diagnostic outputs for piping stdin.
Browse files Browse the repository at this point in the history
  • Loading branch information
taalexander committed Apr 1, 2024
1 parent 758fb41 commit 3480baa
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 20 deletions.
17 changes: 12 additions & 5 deletions include/Frontend/OpenQASM3/QUIRGenQASM3Visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ class QUIRGenQASM3Visitor : public BaseQASM3Visitor {
mlir::OpBuilder circuitParentBuilder;
mlir::ModuleOp newModule;
mlir::quir::CircuitOp currentCircuitOp;
std::string filename;
bool hasFailed{false};
bool buildingInCircuit{false};
uint circuitCount{0};
std::string fileName;
bool requiresParserLocationFix;

mlir::Location getLocation(const QASM::ASTBase *);
bool assign(mlir::Value &, const std::string &);
Expand Down Expand Up @@ -117,17 +118,23 @@ class QUIRGenQASM3Visitor : public BaseQASM3Visitor {

public:
QUIRGenQASM3Visitor(QASM::ASTStatementList *sList, mlir::OpBuilder b,
mlir::ModuleOp newModule, std::string f)
mlir::ModuleOp newModule, std::string fileName,
bool requiresParserLocationFix = false)
: BaseQASM3Visitor(sList), builder(b), topLevelBuilder(b),
circuitParentBuilder(b), newModule(newModule), filename(std::move(f)),
circuitParentBuilder(b), newModule(newModule),
fileName(std::move(fileName)),
requiresParserLocationFix(requiresParserLocationFix),
expression(llvm::createStringError(llvm::inconvertibleErrorCode(),
"Testing error")),
varHandler(builder) {}

QUIRGenQASM3Visitor(mlir::OpBuilder b, mlir::ModuleOp newModule,
std::string filename)
std::string fileName,
bool requiresParserLocationFix = false)
: builder(b), topLevelBuilder(b), circuitParentBuilder(b),
newModule(newModule), filename(std::move(filename)),
fileName(std::move(fileName)),
requiresParserLocationFix(requiresParserLocationFix),
newModule(newModule),
expression(llvm::createStringError(llvm::inconvertibleErrorCode(),
"Testing error")),
varHandler(builder) {}
Expand Down
35 changes: 24 additions & 11 deletions lib/Frontend/OpenQASM3/OpenQASM3Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,14 @@ llvm::Error qssc::frontend::openqasm3::parse(
diagnosticCallback.has_value() ? &diagnosticCallback.value() : nullptr;
sourceMgr_ = &sourceMgr;
QASM::QasmDiagnosticEmitter::SetHandler(
[](const std::string &File, QASM::ASTLocation Loc, // NOLINT
const std::string &Msg, QASM::QasmDiagnosticEmitter::DiagLevel DL) {
[](const std::string &file, QASM::ASTLocation qasmLoc, // NOLINT
const std::string &msg,
QASM::QasmDiagnosticEmitter::DiagLevel qasmDiagLevel) {
qssc::Severity diagLevel = qssc::Severity::Error;
llvm::SourceMgr::DiagKind sourceMgrDiagKind =
llvm::SourceMgr::DiagKind::DK_Error;

switch (DL) {
switch (qasmDiagLevel) {
case QASM::QasmDiagnosticEmitter::DiagLevel::Error:
diagLevel = qssc::Severity::Error;
sourceMgrDiagKind = llvm::SourceMgr::DiagKind::DK_Error;
Expand Down Expand Up @@ -178,13 +179,19 @@ llvm::Error qssc::frontend::openqasm3::parse(
// Capture source context for including it in error messages
assert(sourceMgr_);
auto &sourceMgr = *sourceMgr_;
auto loc = sourceMgr.FindLocForLineAndColumn(1, Loc.LineNo, Loc.ColNo);

auto lineNo = qasmLoc.LineNo;
// Workaround for https://github.com/openqasm/qe-qasm/issues/35
// TODO: Remove once this bug is fixed
if (file == "\"-\"")
lineNo++;

auto loc = sourceMgr.FindLocForLineAndColumn(1, lineNo, qasmLoc.ColNo);

std::string errMsg;
llvm::raw_string_ostream ostream(errMsg);
sourceMgr.PrintMessage(ostream, loc, sourceMgrDiagKind,
"While parsing OpenQASM3 input: \n" + Msg +
"\n");
"While parsing OpenQASM3 input: " + msg);

llvm::errs() << errMsg << "\n";

Expand All @@ -195,14 +202,18 @@ llvm::Error qssc::frontend::openqasm3::parse(
(*diagnosticCallback_)(diag);
}

if (DL == QASM::QasmDiagnosticEmitter::DiagLevel::Error ||
DL == QASM::QasmDiagnosticEmitter::DiagLevel::ICE) {
if (qasmDiagLevel == QASM::QasmDiagnosticEmitter::DiagLevel::Error ||
qasmDiagLevel == QASM::QasmDiagnosticEmitter::DiagLevel::ICE) {
// give up parsing after errors right away
// TODO: update to recent qss-qasm to support continuing
throw std::runtime_error("Failure parsing");
}
});

// Workaround for https://github.com/openqasm/qe-qasm/issues/35
// TODO: Remove once this bug is fixed
bool requiresParserLocationFix = false;

try {
auto sourceFile = sourceBuffer->getBufferIdentifier();

Expand All @@ -212,8 +223,10 @@ llvm::Error qssc::frontend::openqasm3::parse(
if (!(sourceFile == "" || sourceFile == "<stdin>")) {
QASM::QasmPreprocessor::Instance().SetTranslationUnit(sourceFile.str());
root.reset(parser.ParseAST());
} else
} else {
root.reset(parser.ParseAST(sourceBuffer->getBuffer().str()));
requiresParserLocationFix = true;
}

} catch (std::exception &e) {
return llvm::createStringError(
Expand Down Expand Up @@ -252,8 +265,8 @@ llvm::Error qssc::frontend::openqasm3::parse(
QASM::ASTStatementList *statementList =
QASM::ASTStatementBuilder::Instance().List();

qssc::frontend::openqasm3::QUIRGenQASM3Visitor visitor(builder, newModule,
/*filename=*/"");
qssc::frontend::openqasm3::QUIRGenQASM3Visitor visitor(
builder, newModule, "", requiresParserLocationFix);

auto result = parseDurationStr(shotDelay);
if (auto err = result.takeError())
Expand Down
15 changes: 11 additions & 4 deletions lib/Frontend/OpenQASM3/QUIRGenQASM3Visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,15 @@ llvm::cl::opt<bool> debugCircuits("debug-circuits",
} // anonymous namespace

auto QUIRGenQASM3Visitor::getLocation(const ASTBase *node) -> Location {
return mlir::FileLineColLoc::get(builder.getContext(), filename,
node->GetLineNo(), node->GetColNo());

// Workaround for https://github.com/openqasm/qe-qasm/issues/35
// TODO: Remove once this bug is fixed
auto lineNo = node->GetLineNo();
if (requiresParserLocationFix)
lineNo++;

return mlir::FileLineColLoc::get(builder.getContext(), fileName, lineNo,
node->GetColNo());
}

auto QUIRGenQASM3Visitor::assign(Value &val, const std::string &valName)
Expand Down Expand Up @@ -264,7 +271,7 @@ void QUIRGenQASM3Visitor::initialize(
uint numShots, const double &shotDelay,
const mlir::quir::TimeUnits &shotDelayUnits) {
Location const initialLocation =
mlir::FileLineColLoc::get(topLevelBuilder.getContext(), filename, 0, 0);
mlir::FileLineColLoc::get(topLevelBuilder.getContext(), fileName, 0, 0);

// create the "main" function
auto func = topLevelBuilder.create<mlir::func::FuncOp>(
Expand Down Expand Up @@ -326,7 +333,7 @@ void QUIRGenQASM3Visitor::initialize(
}

void QUIRGenQASM3Visitor::setInputFile(std::string fName) {
filename = std::move(fName);
fileName = std::move(fName);
}

mlir::LogicalResult QUIRGenQASM3Visitor::walkAST() {
Expand Down
16 changes: 16 additions & 0 deletions test/Frontend/OpenQASM3/diagnostics-parse-error.qasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
OPENQASM 3.0;
// Ensure piping of input also works with diagnostics.
// TODO: Once https://github.com/openqasm/qe-qasm/issues/35
// is fixed this test will error and the workaround
// in OpenQASM3Frontend.cpp for line number offsets
// will need to be removed for the test to pass.
// RUN: cat %s | ( qss-compiler -X=qasm --emit=mlir || true ) 2>&1 | FileCheck %s --dump-input=fail
// RUN: ( qss-compiler -X=qasm --emit=mlir %s || true ) 2>&1 | FileCheck %s --dump-input=fail

int a;
int b;

a &&& b;
// CHECK: error: While parsing OpenQASM3 input: syntax error, unexpected '&'
// CHECK: a &&& b;
// CHECK: ^
21 changes: 21 additions & 0 deletions test/Frontend/OpenQASM3/diagnostics-unsupported-error.qasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
OPENQASM 3.0;
// Ensure piping of input also works with diagnostics.
// TODO: Once https://github.com/openqasm/qe-qasm/issues/35
// is fixed this test will error and the workaround
// in QUIRGenQASM3Visitor.cpp
// for line number offsets
// will need to be removed for the test to pass.
// RUN: cat %s | ( qss-compiler -X=qasm --emit=mlir || true ) 2>&1 | FileCheck %s --dump-input=fail
// RUN: ( qss-compiler -X=qasm --emit=mlir %s || true ) 2>&1 | FileCheck %s --dump-input=fail

int a;
float b;
int c;
c = float(a) + b;

// CHECK: error: Unsupported cast destination type ASTTypeFloat
// CHECK: c = float(a) + b;
// CHECK: ^
// CHECK: error: Addition is not supported on value of type: 'none'
// CHECK: c = float(a) + b;
// CHECK: ^

0 comments on commit 3480baa

Please sign in to comment.