Skip to content

Enable colors in FileCheck and Swift tests #71449

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

Merged
merged 3 commits into from
Feb 9, 2024
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
55 changes: 54 additions & 1 deletion include/swift/Basic/ColorUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//
//===----------------------------------------------------------------------===//
//
// This file defines an 'OSColor' class for helping printing colorful outputs
// This file defines several utilities for helping print colorful outputs
// to the terminal.
//
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -42,6 +42,59 @@ class OSColor {
OSColor &operator<<(llvm::StringRef Str) { OS << Str; return *this; }
};

/// A stream which forces color output.
class ColoredStream : public raw_ostream {
raw_ostream &Underlying;

public:
explicit ColoredStream(raw_ostream &underlying) : Underlying(underlying) {}
~ColoredStream() override { flush(); }

raw_ostream &changeColor(Colors color, bool bold = false,
bool bg = false) override {
Underlying.changeColor(color, bold, bg);
return *this;
}
raw_ostream &resetColor() override {
Underlying.resetColor();
return *this;
}
raw_ostream &reverseColor() override {
Underlying.reverseColor();
return *this;
}
bool has_colors() const override { return true; }

void write_impl(const char *ptr, size_t size) override {
Underlying.write(ptr, size);
}
uint64_t current_pos() const override {
return Underlying.tell() - GetNumBytesInBuffer();
}

size_t preferred_buffer_size() const override { return 0; }
};

/// A stream which drops all color settings.
class NoColorStream : public raw_ostream {
raw_ostream &Underlying;

public:
explicit NoColorStream(raw_ostream &underlying) : Underlying(underlying) {}
~NoColorStream() override { flush(); }

bool has_colors() const override { return false; }

void write_impl(const char *ptr, size_t size) override {
Underlying.write(ptr, size);
}
uint64_t current_pos() const override {
return Underlying.tell() - GetNumBytesInBuffer();
}

size_t preferred_buffer_size() const override { return 0; }
};

} // namespace swift

#endif // SWIFT_BASIC_COLORUTILS_H
9 changes: 8 additions & 1 deletion include/swift/Frontend/DiagnosticVerifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,16 @@ class DiagnosticVerifier : public DiagnosticConsumer {
SmallVector<unsigned, 4> AdditionalBufferIDs;
bool AutoApplyFixes;
bool IgnoreUnknown;
bool UseColor;
ArrayRef<std::string> AdditionalExpectedPrefixes;

public:
explicit DiagnosticVerifier(SourceManager &SM, ArrayRef<unsigned> BufferIDs,
bool AutoApplyFixes, bool IgnoreUnknown,
bool UseColor,
ArrayRef<std::string> AdditionalExpectedPrefixes)
: SM(SM), BufferIDs(BufferIDs), AutoApplyFixes(AutoApplyFixes),
IgnoreUnknown(IgnoreUnknown),
IgnoreUnknown(IgnoreUnknown), UseColor(UseColor),
AdditionalExpectedPrefixes(AdditionalExpectedPrefixes) {}

void appendAdditionalBufferID(unsigned bufferID) {
Expand All @@ -124,6 +126,11 @@ class DiagnosticVerifier : public DiagnosticConsumer {
bool HadUnexpectedDiag;
};

void printDiagnostic(const llvm::SMDiagnostic &Diag) const;

bool
verifyUnknown(std::vector<CapturedDiagnosticInfo> &CapturedDiagnostics) const;

/// verifyFile - After the file has been processed, check to see if we
/// got all of the expected diagnostics and check to see if there were any
/// unexpected ones.
Expand Down
30 changes: 19 additions & 11 deletions lib/Frontend/DiagnosticVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//===----------------------------------------------------------------------===//

#include "swift/Frontend/DiagnosticVerifier.h"
#include "swift/Basic/ColorUtils.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Parse/Lexer.h"
#include "llvm/ADT/STLExtras.h"
Expand Down Expand Up @@ -328,11 +329,11 @@ static void autoApplyFixes(SourceManager &SM, unsigned BufferID,
if (!error)
outs << Result;
}
} // end anonymous namespace

/// diagnostics for '<unknown>:0' should be considered as unexpected.
static bool
verifyUnknown(SourceManager &SM,
std::vector<CapturedDiagnosticInfo> &CapturedDiagnostics) {
bool DiagnosticVerifier::verifyUnknown(
std::vector<CapturedDiagnosticInfo> &CapturedDiagnostics) const {
bool HadError = false;
for (unsigned i = 0, e = CapturedDiagnostics.size(); i != e; ++i) {
if (CapturedDiagnostics[i].Loc.isValid())
Expand All @@ -346,11 +347,10 @@ verifyUnknown(SourceManager &SM,
.str();

auto diag = SM.GetMessage({}, llvm::SourceMgr::DK_Error, Message, {}, {});
SM.getLLVMSourceMgr().PrintMessage(llvm::errs(), diag);
printDiagnostic(diag);
}
return HadError;
}
} // end anonymous namespace

/// Return true if the given \p ExpectedFixIt is in the fix-its emitted by
/// diagnostic \p D.
Expand All @@ -377,6 +377,13 @@ bool DiagnosticVerifier::checkForFixIt(
return false;
}

void DiagnosticVerifier::printDiagnostic(const llvm::SMDiagnostic &Diag) const {
raw_ostream &stream = llvm::errs();
ColoredStream coloredStream{stream};
raw_ostream &out = UseColor ? coloredStream : stream;
SM.getLLVMSourceMgr().PrintMessage(out, Diag);
}

std::string
DiagnosticVerifier::renderFixits(ArrayRef<CapturedFixItInfo> ActualFixIts,
unsigned BufferID,
Expand Down Expand Up @@ -1184,7 +1191,7 @@ DiagnosticVerifier::Result DiagnosticVerifier::verifyFile(unsigned BufferID) {

// Emit all of the queue'd up errors.
for (auto Err : Errors)
SM.getLLVMSourceMgr().PrintMessage(llvm::errs(), Err);
printDiagnostic(Err);

// If auto-apply fixits is on, rewrite the original source file.
if (AutoApplyFixes)
Expand Down Expand Up @@ -1214,10 +1221,11 @@ void DiagnosticVerifier::printRemainingDiagnostics() const {
break;
}

SM.getLLVMSourceMgr().PrintMessage(
llvm::errs(), getRawLoc(diag.Loc), SMKind,
"diagnostic produced elsewhere: " + diag.Message.str(),
/*Ranges=*/{}, {});
auto message =
SM.GetMessage(diag.Loc, SMKind,
"diagnostic produced elsewhere: " + diag.Message.str(),
/*Ranges=*/{}, {});
printDiagnostic(message);
}
}

Expand Down Expand Up @@ -1288,7 +1296,7 @@ bool DiagnosticVerifier::finishProcessing() {
Result.HadUnexpectedDiag |= FileResult.HadUnexpectedDiag;
}
if (!IgnoreUnknown) {
bool HadError = verifyUnknown(SM, CapturedDiagnostics);
bool HadError = verifyUnknown(CapturedDiagnostics);
Result.HadError |= HadError;
// For <unknown>, all errors are unexpected.
Result.HadUnexpectedDiag |= HadError;
Expand Down
2 changes: 1 addition & 1 deletion lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ bool CompilerInstance::setupDiagnosticVerifierIfNeeded() {
DiagVerifier = std::make_unique<DiagnosticVerifier>(
SourceMgr, InputSourceCodeBufferIDs,
diagOpts.VerifyMode == DiagnosticOptions::VerifyAndApplyFixes,
diagOpts.VerifyIgnoreUnknown,
diagOpts.VerifyIgnoreUnknown, diagOpts.UseColor,
diagOpts.AdditionalDiagnosticVerifierPrefixes);
for (const auto &filename : diagOpts.AdditionalVerifierFiles) {
auto result = getFileSystem().getBufferForFile(filename);
Expand Down
56 changes: 1 addition & 55 deletions lib/Frontend/PrintingDiagnosticConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "swift/AST/ASTBridging.h"
#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/DiagnosticsCommon.h"
#include "swift/Basic/ColorUtils.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Bridging/ASTGen.h"
Expand All @@ -36,61 +37,6 @@ using namespace swift;
using namespace swift::markup;

namespace {
class ColoredStream : public raw_ostream {
raw_ostream &Underlying;
public:
explicit ColoredStream(raw_ostream &underlying) : Underlying(underlying) {}
~ColoredStream() override { flush(); }

raw_ostream &changeColor(Colors color, bool bold = false,
bool bg = false) override {
Underlying.changeColor(color, bold, bg);
return *this;
}
raw_ostream &resetColor() override {
Underlying.resetColor();
return *this;
}
raw_ostream &reverseColor() override {
Underlying.reverseColor();
return *this;
}
bool has_colors() const override {
return true;
}

void write_impl(const char *ptr, size_t size) override {
Underlying.write(ptr, size);
}
uint64_t current_pos() const override {
return Underlying.tell() - GetNumBytesInBuffer();
}

size_t preferred_buffer_size() const override {
return 0;
}
};

/// A stream which drops all color settings.
class NoColorStream : public raw_ostream {
raw_ostream &Underlying;

public:
explicit NoColorStream(raw_ostream &underlying) : Underlying(underlying) {}
~NoColorStream() override { flush(); }

bool has_colors() const override { return false; }

void write_impl(const char *ptr, size_t size) override {
Underlying.write(ptr, size);
}
uint64_t current_pos() const override {
return Underlying.tell() - GetNumBytesInBuffer();
}

size_t preferred_buffer_size() const override { return 0; }
};

// MARK: Markdown Printing
class TerminalMarkupPrinter : public MarkupASTVisitor<TerminalMarkupPrinter> {
llvm::raw_ostream &OS;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/InferViaDefaults.swiftmodule -module-name InferViaDefaults %S/Inputs/type_inference_via_defaults_other_module.swift
// RUN: %target-build-swift -module-name main -Xfrontend -parse-as-library -I %t %s %S/Inputs/type_inference_via_defaults_other_module.swift -o %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s --color
// RUN: %target-run %t/a.out | %FileCheck %s

// REQUIRES: OS=macosx && CPU=x86_64

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift -target %target-cpu-apple-macosx13.0 -parse-as-library -emit-library -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems %S/../Inputs/FakeDistributedActorSystems.swift -o %t/%target-library-name(FakeDistributedActorSystems)
// RUN: %target-build-swift -target %target-cpu-apple-macosx13.0 -parse-as-library -lFakeDistributedActorSystems -module-name main -I %t -L %t %s -o %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s --color
// RUN: %target-run %t/a.out | %FileCheck %s

// REQUIRES: OS=macosx && (CPU=x86_64 || CPU=arm64)
// REQUIRES: executable_test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems %S/../Inputs/FakeDistributedActorSystems.swift
// RUN: %target-build-swift -module-name main -Xfrontend -enable-experimental-distributed -j2 -parse-as-library -I %t %s %S/../Inputs/FakeDistributedActorSystems.swift -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s --color
// RUN: %target-run %t/a.out | %FileCheck %s

// REQUIRES: executable_test
// REQUIRES: concurrency
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems %S/../Inputs/FakeDistributedActorSystems.swift
// RUN: %target-build-swift -module-name main -Xfrontend -enable-experimental-distributed -j2 -parse-as-library -I %t %s %S/../Inputs/FakeDistributedActorSystems.swift -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s --color
// RUN: %target-run %t/a.out | %FileCheck %s

// REQUIRES: executable_test
// REQUIRES: concurrency
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems -disable-availability-checking %S/../Inputs/FakeDistributedActorSystems.swift
// RUN: %target-build-swift -module-name main -Xfrontend -enable-experimental-distributed -Xfrontend -disable-availability-checking -j2 -parse-as-library -I %t %s %S/../Inputs/FakeCodableForDistributedTests.swift %S/../Inputs/FakeDistributedActorSystems.swift -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s --color
// RUN: %target-run %t/a.out | %FileCheck %s

// REQUIRES: executable_test
// REQUIRES: concurrency
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems -disable-availability-checking %S/../Inputs/FakeDistributedActorSystems.swift
// RUN: %target-build-swift -module-name main -O -Xfrontend -disable-availability-checking -j2 -parse-as-library -I %t %s %S/../Inputs/FakeDistributedActorSystems.swift -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s --color
// RUN: %target-run %t/a.out | %FileCheck %s

// REQUIRES: executable_test
// REQUIRES: concurrency
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems -disable-availability-checking %S/../Inputs/FakeDistributedActorSystems.swift
// RUN: %target-build-swift -module-name main -Xfrontend -disable-availability-checking -j2 -parse-as-library -I %t %s %S/../Inputs/FakeDistributedActorSystems.swift -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s --color
// RUN: %target-run %t/a.out | %FileCheck %s

// REQUIRES: executable_test
// REQUIRES: concurrency
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems -disable-availability-checking %S/../Inputs/FakeDistributedActorSystems.swift
// RUN: %target-build-swift -module-name main -Xfrontend -disable-availability-checking -j2 -parse-as-library -I %t %s %S/../Inputs/FakeDistributedActorSystems.swift -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s --color
// RUN: %target-run %t/a.out | %FileCheck %s

// REQUIRES: executable_test
// REQUIRES: concurrency
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems -disable-availability-checking %S/../Inputs/FakeDistributedActorSystems.swift
// RUN: %target-build-swift -module-name main -Xfrontend -disable-availability-checking -j2 -parse-as-library -I %t %s %S/../Inputs/FakeDistributedActorSystems.swift -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s --color
// RUN: %target-run %t/a.out | %FileCheck %s

// REQUIRES: executable_test
// REQUIRES: concurrency
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems -disable-availability-checking %S/../Inputs/FakeDistributedActorSystems.swift
// RUN: %target-build-swift -module-name main -Xfrontend -disable-availability-checking -j2 -parse-as-library -I %t %s %S/../Inputs/FakeDistributedActorSystems.swift -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s --color
// RUN: %target-run %t/a.out | %FileCheck %s

// REQUIRES: executable_test
// REQUIRES: concurrency
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems -disable-availability-checking %S/../Inputs/FakeDistributedActorSystems.swift
// RUN: %target-build-swift -module-name main -Xfrontend -disable-availability-checking -j2 -parse-as-library -I %t %s %S/../Inputs/FakeDistributedActorSystems.swift -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s --color
// RUN: %target-run %t/a.out | %FileCheck %s

// REQUIRES: executable_test
// REQUIRES: concurrency
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems -disable-availability-checking %S/../Inputs/FakeDistributedActorSystems.swift
// RUN: %target-build-swift -module-name main -Xfrontend -disable-availability-checking -j2 -parse-as-library -I %t %s %S/../Inputs/FakeDistributedActorSystems.swift -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s --color
// RUN: %target-run %t/a.out | %FileCheck %s

// REQUIRES: executable_test
// REQUIRES: concurrency
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems -disable-availability-checking %S/../Inputs/FakeDistributedActorSystems.swift
// RUN: %target-build-swift -module-name main -Xfrontend -disable-availability-checking -j2 -parse-as-library -I %t %s %S/../Inputs/FakeDistributedActorSystems.swift -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s --color
// RUN: %target-run %t/a.out | %FileCheck %s

// REQUIRES: executable_test
// REQUIRES: concurrency
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems -disable-availability-checking %S/../Inputs/FakeDistributedActorSystems.swift
// RUN: %target-build-swift -module-name main -Xfrontend -disable-availability-checking -j2 -parse-as-library -I %t %s %S/../Inputs/FakeDistributedActorSystems.swift -o %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s --color
// RUN: %target-run %t/a.out | %FileCheck %s

// REQUIRES: executable_test
// REQUIRES: concurrency
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems -disable-availability-checking %S/../Inputs/FakeDistributedActorSystems.swift
// RUN: %target-build-swift -module-name main -Xfrontend -disable-availability-checking -j2 -parse-as-library -I %t %s %S/../Inputs/FakeDistributedActorSystems.swift -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s --color
// RUN: %target-run %t/a.out | %FileCheck %s

// REQUIRES: executable_test
// REQUIRES: concurrency
Expand Down
Loading