Swift: add location support to TSP diagnostics

This required a bit of an overhaul of the original integration of
JSON diagnostics into binlog.

The problem is that it is quite hard to add a kind of metadata to
binlog entries without changing its code. Another problem is that when
wanting to avoid double evaluation of logging macro arguments one
cannot really add a separate "diagnose" step easily.

The proposed solution consists in two things:
* hook into a binlog plumbing function by providing a better overload
  resolution match, which happens after logging macro expansion,
  bypassing the problem of double evaluation
* in that hook, produce the diagnostic directly, without waiting to
  reconstruct the diagnostics entry from the binlog serialized entry.

This allows to forgo the weird category to diagnostic mapping, and now a
diagnostics emission simply happens when a diagnostic source is given
as the first argument after the log format string. A flavour of
diganostics sources with locations is then added with the same
mechanism, allowing to write something like
```cpp
LOG_ERROR("[{}] ouch!", internalError.withLocation("foo.swift", 32));
```
This commit is contained in:
Paolo Tranquilli
2023-05-11 17:52:02 +02:00
parent 82e780d175
commit 3f2a059b3b
10 changed files with 245 additions and 134 deletions

View File

@@ -12,19 +12,19 @@ static const char* unitTest = "com.apple.product-type.bundle.unit-test";
const std::string_view codeql::programName = "autobuilder";
namespace codeql_diagnostics {
constexpr codeql::SwiftDiagnosticsSource no_project_found{
"no_project_found", "No Xcode project or workspace detected", customizingBuildAction,
customizingBuildHelpLinks};
namespace codeql {
constexpr SwiftDiagnosticsSource noProjectFound{"no-project-found",
"No Xcode project or workspace detected",
customizingBuildAction, customizingBuildHelpLinks};
constexpr codeql::SwiftDiagnosticsSource no_swift_target{
"no_swift_target", "No Swift compilation target found", customizingBuildAction,
customizingBuildHelpLinks};
constexpr SwiftDiagnosticsSource noSwiftTarget{"no-swift-target",
"No Swift compilation target found",
customizingBuildAction, customizingBuildHelpLinks};
constexpr codeql::SwiftDiagnosticsSource spm_not_supported{
"spm_not_supported", "Swift Package Manager build unsupported by autobuild",
constexpr SwiftDiagnosticsSource spmNotSupported{
"spm-not-supported", "Swift Package Manager build unsupported by autobuild",
customizingBuildAction, customizingBuildHelpLinks};
} // namespace codeql_diagnostics
} // namespace codeql
static codeql::Logger& logger() {
static codeql::Logger ret{"main"};
@@ -53,14 +53,15 @@ static void autobuild(const CLIArgs& args) {
std::sort(std::begin(targets), std::end(targets),
[](Target& lhs, Target& rhs) { return lhs.fileCount > rhs.fileCount; });
if ((!collected.xcodeEncountered || targets.empty()) && collected.swiftPackageEncountered) {
DIAGNOSE_ERROR(spm_not_supported,
"No viable Swift Xcode target was found but a Swift package was detected. Swift "
"Package Manager builds are not yet supported by the autobuilder");
LOG_ERROR("[{}] No viable Swift Xcode target was found but a Swift package was detected. Swift "
"Package Manager builds are not yet supported by the autobuilder",
codeql::spmNotSupported);
} else if (!collected.xcodeEncountered) {
DIAGNOSE_ERROR(no_project_found, "No Xcode project or workspace was found");
LOG_ERROR("[{}] No Xcode project or workspace was found", codeql::noProjectFound);
} else if (targets.empty()) {
DIAGNOSE_ERROR(no_swift_target, "All targets found within Xcode projects or workspaces either "
"have no Swift sources or are tests");
LOG_ERROR("[{}] All targets found within Xcode projects or workspaces either "
"have no Swift sources or are tests",
codeql::noSwiftTarget);
} else {
LOG_INFO("Selected {}", targets.front());
buildTarget(targets.front(), args.dryRun);