-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
182 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (c) 2024, Trail of Bits, Inc. | ||
Check notice on line 1 in include/vast/Frontend/Sarif.hpp GitHub Actions / cpp-linter (18, 22.04)Run clang-format on include/vast/Frontend/Sarif.hpp
|
||
|
||
#pragma once | ||
|
||
|
||
#ifdef VAST_ENABLE_SARIF | ||
#include <gap/sarif/sarif.hpp> | ||
|
||
#include "vast/Frontend/Options.hpp" | ||
#include "vast/Util/Common.hpp" | ||
|
||
namespace vast::cc::sarif { | ||
|
||
gap::sarif::location mk_location(loc_t loc); | ||
gap::sarif::location mk_location(file_loc_t loc); | ||
gap::sarif::location mk_location(name_loc_t loc); | ||
|
||
gap::sarif::physical_location get_physical_loc(file_loc_t loc); | ||
|
||
gap::sarif::level get_severity_level(mlir::DiagnosticSeverity severity); | ||
|
||
struct diagnostics { | ||
gap::sarif::run run; | ||
|
||
explicit diagnostics(const vast_args &vargs); | ||
|
||
auto handler() { | ||
return [&] (auto &diag) { | ||
gap::sarif::result result = { | ||
.ruleId = "mlir-diag", | ||
.message = { .text = diag.str() } | ||
}; | ||
|
||
if (auto loc = mk_location(diag.getLocation()); loc.physicalLocation.has_value()) { | ||
result.locations.push_back(std::move(loc)); | ||
} | ||
|
||
result.level = get_severity_level(diag.getSeverity()); | ||
run.results.push_back(std::move(result)); | ||
}; | ||
}; | ||
|
||
gap::sarif::root emit(logical_result result) && { | ||
run.invocations[0].executionSuccessful = mlir::succeeded(result); | ||
return { | ||
.version = gap::sarif::version::k2_1_0, | ||
.runs{ std::move(run) }, | ||
}; | ||
} | ||
}; | ||
|
||
} // namespace vast::cc::sarif | ||
#endif // VAST_ENABLE_SARIF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (c) 2024, Trail of Bits, Inc. | ||
Check notice on line 1 in lib/vast/Frontend/Sarif.cpp GitHub Actions / cpp-linter (18, 22.04)Run clang-format on lib/vast/Frontend/Sarif.cpp
|
||
|
||
#include "vast/Frontend/Sarif.hpp" | ||
|
||
#ifdef VAST_ENABLE_SARIF | ||
|
||
#include "vast/Config/config.h" | ||
|
||
namespace vast::cc::sarif { | ||
|
||
gap::sarif::location mk_location(loc_t loc) { | ||
if (auto file_loc = mlir::dyn_cast< file_loc_t >(loc)) { | ||
return mk_location(file_loc); | ||
} else if (auto name_loc = mlir::dyn_cast< name_loc_t >(loc)) { | ||
return mk_location(name_loc); | ||
} | ||
|
||
return {}; | ||
} | ||
|
||
gap::sarif::location mk_location(file_loc_t loc) { | ||
return { | ||
.physicalLocation{ get_physical_loc(loc) }, | ||
}; | ||
} | ||
|
||
gap::sarif::location mk_location(name_loc_t loc) { | ||
return { | ||
.physicalLocation{ | ||
get_physical_loc(mlir::cast< file_loc_t >(loc.getChildLoc())) | ||
}, | ||
.logicalLocations{ { .name = loc.getName().str() } }, | ||
}; | ||
} | ||
|
||
gap::sarif::physical_location get_physical_loc(file_loc_t loc) { | ||
std::filesystem::path file_path{ loc.getFilename().str() }; | ||
auto abs_path = std::filesystem::absolute(file_path); | ||
return { | ||
.artifactLocation{ { .uri{ "file://" + abs_path.string() } } }, | ||
.region{ { | ||
.startLine = loc.getLine(), | ||
.startColumn = loc.getColumn(), | ||
} }, | ||
}; | ||
} | ||
|
||
gap::sarif::level get_severity_level(mlir::DiagnosticSeverity severity) { | ||
using enum gap::sarif::level; | ||
using enum mlir::DiagnosticSeverity; | ||
switch (severity) { | ||
case Note: return kNote; | ||
case Warning: return kWarning; | ||
case Error: return kError; | ||
case Remark: return kNote; | ||
} | ||
} | ||
|
||
diagnostics::diagnostics(const vast_args &vargs) | ||
: run(gap::sarif::run{ | ||
.tool{ | ||
.driver{ | ||
.name = "vast-front", | ||
.organization = "Trail of Bits, inc.", | ||
.product = "VAST", | ||
.version = std::string{ vast::version }, | ||
.informationUri = std::string{ vast::homepage_url }, | ||
}, | ||
}, | ||
.invocations{{ | ||
.arguments{ std::begin(vargs.args), std::end(vargs.args) }, | ||
.executionSuccessful = true, | ||
}} | ||
}) | ||
{} | ||
|
||
} // namespace vast::cc::sarif | ||
|
||
#endif // VAST_ENABLE_SARIF |