14 changes: 7 additions & 7 deletions flang/tools/f18/dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@

// This file defines Dump routines available for calling from the debugger.
// Each is based on operator<< for that type. There are overloadings for
// reference and pointer, and for dumping to a provided ostream or cerr.
// reference and pointer, and for dumping to a provided raw_ostream or errs().

#ifdef DEBUGF18

#include <iostream>
#include "llvm/Support/raw_ostream.h"

#define DEFINE_DUMP(ns, name) \
namespace ns { \
class name; \
std::ostream &operator<<(std::ostream &, const name &); \
llvm::raw_ostream &operator<<(llvm::raw_ostream &, const name &); \
} \
void Dump(std::ostream &os, const ns::name &x) { os << x << '\n'; } \
void Dump(std::ostream &os, const ns::name *x) { \
void Dump(llvm::raw_ostream &os, const ns::name &x) { os << x << '\n'; } \
void Dump(llvm::raw_ostream &os, const ns::name *x) { \
if (x == nullptr) \
os << "null\n"; \
else \
Dump(os, *x); \
} \
void Dump(const ns::name &x) { Dump(std::cerr, x); } \
void Dump(const ns::name *x) { Dump(std::cerr, *x); }
void Dump(const ns::name &x) { Dump(llvm::errs(), x); } \
void Dump(const ns::name *x) { Dump(llvm::errs(), *x); }

namespace Fortran {
DEFINE_DUMP(parser, Name)
Expand Down
53 changes: 29 additions & 24 deletions flang/tools/f18/f18-parse-demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
#include "flang/Parser/parsing.h"
#include "flang/Parser/provenance.h"
#include "flang/Parser/unparse.h"
#include <cerrno>
#include "llvm/Support/Errno.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <memory>
#include <optional>
Expand Down Expand Up @@ -112,14 +113,14 @@ bool ParentProcess() {
void Exec(std::vector<char *> &argv, bool verbose = false) {
if (verbose) {
for (size_t j{0}; j < argv.size(); ++j) {
std::cerr << (j > 0 ? " " : "") << argv[j];
llvm::errs() << (j > 0 ? " " : "") << argv[j];
}
std::cerr << '\n';
llvm::errs() << '\n';
}
argv.push_back(nullptr);
execvp(argv[0], &argv[0]);
std::cerr << "execvp(" << argv[0] << ") failed: " << std::strerror(errno)
<< '\n';
llvm::errs() << "execvp(" << argv[0]
<< ") failed: " << llvm::sys::StrError(errno) << '\n';
exit(EXIT_FAILURE);
}

Expand Down Expand Up @@ -173,52 +174,52 @@ std::string CompileFortran(
parsing.Prescan(path, options);
if (!parsing.messages().empty() &&
(driver.warningsAreErrors || parsing.messages().AnyFatalError())) {
std::cerr << driver.prefix << "could not scan " << path << '\n';
parsing.messages().Emit(std::cerr, parsing.cooked());
llvm::errs() << driver.prefix << "could not scan " << path << '\n';
parsing.messages().Emit(llvm::errs(), parsing.cooked());
exitStatus = EXIT_FAILURE;
return {};
}
if (driver.dumpProvenance) {
parsing.DumpProvenance(std::cout);
parsing.DumpProvenance(llvm::outs());
return {};
}
if (driver.dumpCookedChars) {
parsing.DumpCookedChars(std::cout);
parsing.DumpCookedChars(llvm::outs());
return {};
}
parsing.Parse(&std::cout);
parsing.Parse(llvm::outs());
auto stop{CPUseconds()};
if (driver.timeParse) {
if (canTime) {
std::cout << "parse time for " << path << ": " << (stop - start)
<< " CPU seconds\n";
llvm::outs() << "parse time for " << path << ": " << (stop - start)
<< " CPU seconds\n";
} else {
std::cout << "no timing information due to lack of clock_gettime()\n";
llvm::outs() << "no timing information due to lack of clock_gettime()\n";
}
}

parsing.ClearLog();
parsing.messages().Emit(std::cerr, parsing.cooked());
parsing.messages().Emit(llvm::errs(), parsing.cooked());
if (!parsing.consumedWholeFile()) {
parsing.EmitMessage(
std::cerr, parsing.finalRestingPlace(), "parser FAIL (final position)");
parsing.EmitMessage(llvm::errs(), parsing.finalRestingPlace(),
"parser FAIL (final position)");
exitStatus = EXIT_FAILURE;
return {};
}
if ((!parsing.messages().empty() &&
(driver.warningsAreErrors || parsing.messages().AnyFatalError())) ||
!parsing.parseTree()) {
std::cerr << driver.prefix << "could not parse " << path << '\n';
llvm::errs() << driver.prefix << "could not parse " << path << '\n';
exitStatus = EXIT_FAILURE;
return {};
}
auto &parseTree{*parsing.parseTree()};
if (driver.dumpParseTree) {
Fortran::parser::DumpTree(std::cout, parseTree);
Fortran::parser::DumpTree(llvm::outs(), parseTree);
return {};
}
if (driver.dumpUnparse) {
Unparse(std::cout, parseTree, driver.encoding, true /*capitalize*/,
Unparse(llvm::outs(), parseTree, driver.encoding, true /*capitalize*/,
options.features.IsEnabled(
Fortran::common::LanguageFeature::BackslashEscapes));
return {};
Expand All @@ -233,8 +234,12 @@ std::string CompileFortran(
std::snprintf(tmpSourcePath, sizeof tmpSourcePath, "/tmp/f18-%lx.f90",
static_cast<unsigned long>(getpid()));
{
std::ofstream tmpSource;
tmpSource.open(tmpSourcePath);
std::error_code EC;
llvm::raw_fd_ostream tmpSource(tmpSourcePath, EC, llvm::sys::fs::F_None);
if (EC) {
llvm::errs() << EC.message();
std::exit(EXIT_FAILURE);
}
Unparse(tmpSource, parseTree, driver.encoding, true /*capitalize*/,
options.features.IsEnabled(
Fortran::common::LanguageFeature::BackslashEscapes));
Expand Down Expand Up @@ -402,7 +407,7 @@ int main(int argc, char *const argv[]) {
} else if (arg == "-i8" || arg == "-fdefault-integer-8") {
defaultKinds.set_defaultIntegerKind(8);
} else if (arg == "-help" || arg == "--help" || arg == "-?") {
std::cerr
llvm::errs()
<< "f18-parse-demo options:\n"
<< " -Mfixed | -Mfree force the source form\n"
<< " -Mextend 132-column fixed form\n"
Expand All @@ -425,7 +430,7 @@ int main(int argc, char *const argv[]) {
<< "Other options are passed through to the $F18_FC compiler.\n";
return exitStatus;
} else if (arg == "-V") {
std::cerr << "\nf18-parse-demo\n";
llvm::errs() << "\nf18-parse-demo\n";
return exitStatus;
} else {
driver.fcArgs.push_back(arg);
Expand Down
100 changes: 53 additions & 47 deletions flang/tools/f18/f18.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
#include "flang/Semantics/expression.h"
#include "flang/Semantics/semantics.h"
#include "flang/Semantics/unparse-with-symbols.h"
#include "llvm/Support/Errno.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <memory>
#include <optional>
Expand Down Expand Up @@ -58,8 +58,9 @@ struct MeasurementVisitor {
void MeasureParseTree(const Fortran::parser::Program &program) {
MeasurementVisitor visitor;
Fortran::parser::Walk(program, visitor);
std::cout << "Parse tree comprises " << visitor.objects
<< " objects and occupies " << visitor.bytes << " total bytes.\n";
llvm::outs() << "Parse tree comprises " << visitor.objects
<< " objects and occupies " << visitor.bytes
<< " total bytes.\n";
}

std::vector<std::string> filesToDelete;
Expand Down Expand Up @@ -123,14 +124,14 @@ bool ParentProcess() {
void Exec(std::vector<char *> &argv, bool verbose = false) {
if (verbose) {
for (size_t j{0}; j < argv.size(); ++j) {
std::cerr << (j > 0 ? " " : "") << argv[j];
llvm::errs() << (j > 0 ? " " : "") << argv[j];
}
std::cerr << '\n';
llvm::errs() << '\n';
}
argv.push_back(nullptr);
execvp(argv[0], &argv[0]);
std::cerr << "execvp(" << argv[0] << ") failed: " << std::strerror(errno)
<< '\n';
llvm::errs() << "execvp(" << argv[0]
<< ") failed: " << llvm::sys::StrError(errno) << '\n';
exit(EXIT_FAILURE);
}

Expand Down Expand Up @@ -168,21 +169,22 @@ std::string RelocatableName(const DriverOptions &driver, std::string path) {
int exitStatus{EXIT_SUCCESS};

static Fortran::parser::AnalyzedObjectsAsFortran asFortran{
[](std::ostream &o, const Fortran::evaluate::GenericExprWrapper &x) {
[](llvm::raw_ostream &o, const Fortran::evaluate::GenericExprWrapper &x) {
if (x.v) {
x.v->AsFortran(o);
} else {
o << "(bad expression)";
}
},
[](std::ostream &o, const Fortran::evaluate::GenericAssignmentWrapper &x) {
[](llvm::raw_ostream &o,
const Fortran::evaluate::GenericAssignmentWrapper &x) {
if (x.v) {
x.v->AsFortran(o);
} else {
o << "(bad assignment)";
}
},
[](std::ostream &o, const Fortran::evaluate::ProcedureRef &x) {
[](llvm::raw_ostream &o, const Fortran::evaluate::ProcedureRef &x) {
x.AsFortran(o << "CALL ");
},
};
Expand Down Expand Up @@ -211,37 +213,37 @@ std::string CompileFortran(std::string path, Fortran::parser::Options options,
parsing.Prescan(path, options);
if (!parsing.messages().empty() &&
(driver.warningsAreErrors || parsing.messages().AnyFatalError())) {
std::cerr << driver.prefix << "could not scan " << path << '\n';
parsing.messages().Emit(std::cerr, parsing.cooked());
llvm::errs() << driver.prefix << "could not scan " << path << '\n';
parsing.messages().Emit(llvm::errs(), parsing.cooked());
exitStatus = EXIT_FAILURE;
return {};
}
if (driver.dumpProvenance) {
parsing.DumpProvenance(std::cout);
parsing.DumpProvenance(llvm::outs());
return {};
}
if (driver.dumpCookedChars) {
parsing.messages().Emit(std::cerr, parsing.cooked());
parsing.DumpCookedChars(std::cout);
parsing.messages().Emit(llvm::errs(), parsing.cooked());
parsing.DumpCookedChars(llvm::outs());
return {};
}
parsing.Parse(&std::cout);
parsing.Parse(llvm::outs());
if (options.instrumentedParse) {
parsing.DumpParsingLog(std::cout);
parsing.DumpParsingLog(llvm::outs());
return {};
}
parsing.ClearLog();
parsing.messages().Emit(std::cerr, parsing.cooked());
parsing.messages().Emit(llvm::errs(), parsing.cooked());
if (!parsing.consumedWholeFile()) {
parsing.EmitMessage(
std::cerr, parsing.finalRestingPlace(), "parser FAIL (final position)");
parsing.EmitMessage(llvm::errs(), parsing.finalRestingPlace(),
"parser FAIL (final position)");
exitStatus = EXIT_FAILURE;
return {};
}
if ((!parsing.messages().empty() &&
(driver.warningsAreErrors || parsing.messages().AnyFatalError())) ||
!parsing.parseTree()) {
std::cerr << driver.prefix << "could not parse " << path << '\n';
llvm::errs() << driver.prefix << "could not parse " << path << '\n';
exitStatus = EXIT_FAILURE;
return {};
}
Expand All @@ -255,58 +257,58 @@ std::string CompileFortran(std::string path, Fortran::parser::Options options,
Fortran::semantics::Semantics semantics{semanticsContext, parseTree,
parsing.cooked(), driver.debugModuleWriter};
semantics.Perform();
semantics.EmitMessages(std::cerr);
semantics.EmitMessages(llvm::errs());
if (driver.dumpSymbols) {
semantics.DumpSymbols(std::cout);
semantics.DumpSymbols(llvm::outs());
}
if (semantics.AnyFatalError()) {
std::cerr << driver.prefix << "semantic errors in " << path << '\n';
llvm::errs() << driver.prefix << "semantic errors in " << path << '\n';
exitStatus = EXIT_FAILURE;
if (driver.dumpParseTree) {
Fortran::parser::DumpTree(std::cout, parseTree, &asFortran);
Fortran::parser::DumpTree(llvm::outs(), parseTree, &asFortran);
}
return {};
}
if (driver.dumpUnparseWithSymbols) {
Fortran::semantics::UnparseWithSymbols(
std::cout, parseTree, driver.encoding);
llvm::outs(), parseTree, driver.encoding);
return {};
}
if (driver.getSymbolsSources) {
semantics.DumpSymbolsSources(std::cout);
semantics.DumpSymbolsSources(llvm::outs());
return {};
}
if (driver.getDefinition) {
if (auto cb{parsing.cooked().GetCharBlockFromLineAndColumns(
driver.getDefinitionArgs.line,
driver.getDefinitionArgs.startColumn,
driver.getDefinitionArgs.endColumn)}) {
std::cerr << "String range: >" << cb->ToString() << "<\n";
llvm::errs() << "String range: >" << cb->ToString() << "<\n";
if (auto symbol{semanticsContext.FindScope(*cb).FindSymbol(*cb)}) {
std::cerr << "Found symbol name: " << symbol->name().ToString()
<< "\n";
llvm::errs() << "Found symbol name: " << symbol->name().ToString()
<< "\n";
if (auto sourceInfo{
parsing.cooked().GetSourcePositionRange(symbol->name())}) {
std::cout << symbol->name().ToString() << ": "
<< sourceInfo->first.file.path() << ", "
<< sourceInfo->first.line << ", "
<< sourceInfo->first.column << "-"
<< sourceInfo->second.column << "\n";
llvm::outs() << symbol->name().ToString() << ": "
<< sourceInfo->first.file.path() << ", "
<< sourceInfo->first.line << ", "
<< sourceInfo->first.column << "-"
<< sourceInfo->second.column << "\n";
exitStatus = EXIT_SUCCESS;
return {};
}
}
}
std::cerr << "Symbol not found.\n";
llvm::errs() << "Symbol not found.\n";
exitStatus = EXIT_FAILURE;
return {};
}
}
if (driver.dumpParseTree) {
Fortran::parser::DumpTree(std::cout, parseTree, &asFortran);
Fortran::parser::DumpTree(llvm::outs(), parseTree, &asFortran);
}
if (driver.dumpUnparse) {
Unparse(std::cout, parseTree, driver.encoding, true /*capitalize*/,
Unparse(llvm::outs(), parseTree, driver.encoding, true /*capitalize*/,
options.features.IsEnabled(
Fortran::common::LanguageFeature::BackslashEscapes),
nullptr /* action before each statement */, &asFortran);
Expand All @@ -317,7 +319,7 @@ std::string CompileFortran(std::string path, Fortran::parser::Options options,
Fortran::lower::annotateControl(*ast);
Fortran::lower::dumpPFT(llvm::outs(), *ast);
} else {
std::cerr << "Pre FIR Tree is NULL.\n";
llvm::errs() << "Pre FIR Tree is NULL.\n";
exitStatus = EXIT_FAILURE;
}
}
Expand All @@ -331,8 +333,12 @@ std::string CompileFortran(std::string path, Fortran::parser::Options options,
std::snprintf(tmpSourcePath, sizeof tmpSourcePath, "/tmp/f18-%lx.f90",
static_cast<unsigned long>(getpid()));
{
std::ofstream tmpSource;
tmpSource.open(tmpSourcePath);
std::error_code EC;
llvm::raw_fd_ostream tmpSource(tmpSourcePath, EC, llvm::sys::fs::F_None);
if (EC) {
llvm::errs() << EC.message() << "\n";
std::exit(EXIT_FAILURE);
}
Unparse(tmpSource, parseTree, driver.encoding, true /*capitalize*/,
options.features.IsEnabled(
Fortran::common::LanguageFeature::BackslashEscapes),
Expand Down Expand Up @@ -558,13 +564,13 @@ int main(int argc, char *const argv[]) {
int arguments[3];
for (int i = 0; i < 3; i++) {
if (args.empty()) {
std::cerr << "Must provide 3 arguments for -fget-definitions.\n";
llvm::errs() << "Must provide 3 arguments for -fget-definitions.\n";
return EXIT_FAILURE;
}
arguments[i] = std::strtol(args.front().c_str(), &endptr, 10);
if (*endptr != '\0') {
std::cerr << "Invalid argument to -fget-definitions: " << args.front()
<< '\n';
llvm::errs() << "Invalid argument to -fget-definitions: "
<< args.front() << '\n';
return EXIT_FAILURE;
}
args.pop_front();
Expand All @@ -573,7 +579,7 @@ int main(int argc, char *const argv[]) {
} else if (arg == "-fget-symbols-sources") {
driver.getSymbolsSources = true;
} else if (arg == "-help" || arg == "--help" || arg == "-?") {
std::cerr
llvm::errs()
<< "f18 options:\n"
<< " -Mfixed | -Mfree force the source form\n"
<< " -Mextend 132-column fixed form\n"
Expand Down Expand Up @@ -608,7 +614,7 @@ int main(int argc, char *const argv[]) {
<< "Other options are passed through to the compiler.\n";
return exitStatus;
} else if (arg == "-V") {
std::cerr << "\nf18 compiler (under development)\n";
llvm::errs() << "\nf18 compiler (under development)\n";
return exitStatus;
} else {
driver.pgf90Args.push_back(arg);
Expand Down
2 changes: 2 additions & 0 deletions flang/unittests/Decimal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add_executable(quick-sanity-test

target_link_libraries(quick-sanity-test
FortranDecimal
LLVMSupport
)

add_executable(thorough-test
Expand All @@ -20,6 +21,7 @@ add_executable(thorough-test

target_link_libraries(thorough-test
FortranDecimal
LLVMSupport
)

add_test(Sanity quick-sanity-test)
19 changes: 9 additions & 10 deletions flang/unittests/Decimal/quick-sanity-test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "flang/Decimal/decimal.h"
#include "llvm/Support/raw_ostream.h"
#include <cinttypes>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace Fortran::decimal;

Expand All @@ -14,11 +14,12 @@ union u {
std::uint32_t u;
};

std::ostream &failed(float x) {
llvm::raw_ostream &failed(float x) {
++fails;
union u u;
u.x = x;
return std::cout << "FAIL: 0x" << std::hex << u.u << std::dec;
llvm::outs() << "FAIL: 0x";
return llvm::outs().write_hex(u.u);
}

void testDirect(float x, const char *expect, int expectExpo, int flags = 0) {
Expand Down Expand Up @@ -60,15 +61,13 @@ void testReadback(float x, int flags) {
if (!(x == x)) {
if (y == y || *p != '\0' || (rflags & Invalid)) {
u.x = y;
failed(x) << " (NaN) " << flags << ": -> '" << result.str << "' -> 0x"
<< std::hex << u.u << std::dec << " '" << p << "' " << rflags
<< '\n';
failed(x) << " (NaN) " << flags << ": -> '" << result.str << "' -> 0x";
failed(x).write_hex(u.u) << " '" << p << "' " << rflags << '\n';
}
} else if (x != y || *p != '\0' || (rflags & Invalid)) {
u.x = y;
failed(x) << ' ' << flags << ": -> '" << result.str << "' -> 0x"
<< std::hex << u.u << std::dec << " '" << p << "' " << rflags
<< '\n';
failed(x) << ' ' << flags << ": -> '" << result.str << "' -> 0x";
failed(x).write_hex(u.u) << " '" << p << "' " << rflags << '\n';
}
}
}
Expand Down Expand Up @@ -138,6 +137,6 @@ int main() {
testReadback(u.x, Minimize);
testReadback(-u.x, Minimize);
}
std::cout << tests << " tests run, " << fails << " tests failed\n";
llvm::outs() << tests << " tests run, " << fails << " tests failed\n";
return fails > 0;
}
24 changes: 12 additions & 12 deletions flang/unittests/Decimal/thorough-test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "flang/Decimal/decimal.h"
#include "llvm/Support/raw_ostream.h"
#include <cinttypes>
#include <cstdio>
#include <cstring>
#include <iostream>

static constexpr int incr{1}; // steps through all values
static constexpr bool doNegative{true};
Expand All @@ -18,21 +18,23 @@ union u {
std::uint32_t u;
};

std::ostream &failed(float x) {
llvm::raw_ostream &failed(float x) {
++fails;
union u u;
u.x = x;
return std::cout << "FAIL: 0x" << std::hex << u.u << std::dec;
llvm::outs() << "FAIL: 0x";
return llvm::outs().write_hex(u.u);
}

void testReadback(float x, int flags) {
char buffer[1024];
union u u;
u.x = x;
if (!(tests & 0x3fffff)) {
std::cerr << "\n0x" << std::hex << u.u << std::dec << ' ';
llvm::errs() << "\n0x";
llvm::errs().write_hex(u.u) << ' ';
} else if (!(tests & 0xffff)) {
std::cerr << '.';
llvm::errs() << '.';
}
++tests;
auto result{ConvertFloatToDecimal(buffer, sizeof buffer,
Expand All @@ -56,15 +58,13 @@ void testReadback(float x, int flags) {
if (!(x == x)) {
if (y == y || *p != '\0' || (rflags & Invalid)) {
u.x = y;
failed(x) << " (NaN) " << flags << ": -> '" << result.str << "' -> 0x"
<< std::hex << u.u << std::dec << " '" << p << "' " << rflags
<< '\n';
failed(x) << " (NaN) " << flags << ": -> '" << result.str << "' -> 0x";
failed(x).write_hex(u.u) << " '" << p << "' " << rflags << '\n';
}
} else if (x != y || *p != '\0' || (rflags & Invalid)) {
u.x = y;
failed(x) << ' ' << flags << ": -> '" << result.str << "' -> 0x"
<< std::hex << u.u << std::dec << " '" << p << "' " << rflags
<< '\n';
failed(x) << ' ' << flags << ": -> '" << result.str << "' -> 0x";
failed(x).write_hex(u.u) << " '" << p << "' " << rflags << '\n';
}
}
}
Expand All @@ -83,6 +83,6 @@ int main() {
}
}
}
std::cout << '\n' << tests << " tests run, " << fails << " tests failed\n";
llvm::outs() << '\n' << tests << " tests run, " << fails << " tests failed\n";
return fails > 0;
}
11 changes: 11 additions & 0 deletions flang/unittests/Evaluate/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ add_executable(leading-zero-bit-count-test

target_link_libraries(leading-zero-bit-count-test
FortranEvaluateTesting
LLVMSupport
)

add_executable(bit-population-count-test
Expand All @@ -25,6 +26,7 @@ add_executable(bit-population-count-test

target_link_libraries(bit-population-count-test
FortranEvaluateTesting
LLVMSupport
)

add_executable(uint128-test
Expand All @@ -33,6 +35,7 @@ add_executable(uint128-test

target_link_libraries(uint128-test
FortranEvaluateTesting
LLVMSupport
)

# These routines live in lib/Common but we test them here.
Expand All @@ -49,6 +52,7 @@ target_link_libraries(expression-test
FortranEvaluate
FortranSemantics
FortranParser
LLVMSupport
)

add_executable(integer-test
Expand All @@ -59,6 +63,7 @@ target_link_libraries(integer-test
FortranEvaluateTesting
FortranEvaluate
FortranSemantics
LLVMSupport
)

add_executable(intrinsics-test
Expand All @@ -72,6 +77,7 @@ target_link_libraries(intrinsics-test
FortranSemantics
FortranParser
FortranRuntime
LLVMSupport
)

add_executable(logical-test
Expand All @@ -82,6 +88,7 @@ target_link_libraries(logical-test
FortranEvaluateTesting
FortranEvaluate
FortranSemantics
LLVMSupport
)

# GCC -fno-exceptions breaks the fenv.h interfaces needed to capture
Expand All @@ -98,6 +105,7 @@ target_link_libraries(real-test
FortranEvaluate
FortranDecimal
FortranSemantics
LLVMSupport
)

add_executable(reshape-test
Expand All @@ -109,6 +117,7 @@ target_link_libraries(reshape-test
FortranSemantics
FortranEvaluate
FortranRuntime
LLVMSupport
)

add_executable(ISO-Fortran-binding-test
Expand All @@ -120,6 +129,7 @@ target_link_libraries(ISO-Fortran-binding-test
FortranEvaluate
FortranSemantics
FortranRuntime
LLVMSupport
)

add_executable(folding-test
Expand All @@ -130,6 +140,7 @@ target_link_libraries(folding-test
FortranEvaluateTesting
FortranEvaluate
FortranSemantics
LLVMSupport
)

add_test(Expression expression-test)
Expand Down
18 changes: 8 additions & 10 deletions flang/unittests/Evaluate/ISO-Fortran-binding.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#include "testing.h"
#include "../../include/flang/ISO_Fortran_binding.h"
#include "../../runtime/descriptor.h"
#include "llvm/Support/raw_ostream.h"
#include <type_traits>
#ifdef VERBOSE
#include <iostream>
#endif

using namespace Fortran::runtime;
using namespace Fortran::ISO;
Expand Down Expand Up @@ -71,13 +69,13 @@ static void AddNoiseToCdesc(CFI_cdesc_t *dv, CFI_rank_t rank) {
static void DumpTestWorld(const void *bAddr, CFI_attribute_t attr,
CFI_type_t ty, std::size_t eLen, CFI_rank_t rank,
const CFI_index_t *eAddr) {
std::cout << " base_addr: " << std::hex
<< reinterpret_cast<std::intptr_t>(bAddr)
<< " attribute: " << static_cast<int>(attr) << std::dec
<< " type: " << static_cast<int>(ty) << " elem_len: " << eLen
<< " rank: " << static_cast<int>(rank) << " extent: " << std::hex
<< reinterpret_cast<std::intptr_t>(eAddr) << std::endl
<< std::dec;
llvm::outs() << " base_addr: ";
llvm::outs().write_hex(reinterpret_cast<std::intptr_t>(bAddr))
<< " attribute: " << static_cast<int>(attr)
<< " type: " << static_cast<int>(ty) << " elem_len: " << eLen
<< " rank: " << static_cast<int>(rank) << " extent: ";
llvm::outs().write_hex(reinterpret_cast<std::intptr_t>(eAddr)) << '\n';
llvm::outs().flush();
}
#endif

Expand Down
13 changes: 9 additions & 4 deletions flang/unittests/Evaluate/fp-testing.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "fp-testing.h"
#include "llvm/Support/Errno.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
Expand All @@ -15,11 +16,13 @@ ScopedHostFloatingPointEnvironment::ScopedHostFloatingPointEnvironment(
) {
errno = 0;
if (feholdexcept(&originalFenv_) != 0) {
std::fprintf(stderr, "feholdexcept() failed: %s\n", std::strerror(errno));
std::fprintf(stderr, "feholdexcept() failed: %s\n",
llvm::sys::StrError(errno).c_str());
std::abort();
}
if (fegetenv(&currentFenv_) != 0) {
std::fprintf(stderr, "fegetenv() failed: %s\n", std::strerror(errno));
std::fprintf(
stderr, "fegetenv() failed: %s\n", llvm::sys::StrError(errno).c_str());
std::abort();
}
#if __x86_64__
Expand All @@ -38,15 +41,17 @@ ScopedHostFloatingPointEnvironment::ScopedHostFloatingPointEnvironment(
#endif
errno = 0;
if (fesetenv(&currentFenv_) != 0) {
std::fprintf(stderr, "fesetenv() failed: %s\n", std::strerror(errno));
std::fprintf(
stderr, "fesetenv() failed: %s\n", llvm::sys::StrError(errno).c_str());
std::abort();
}
}

ScopedHostFloatingPointEnvironment::~ScopedHostFloatingPointEnvironment() {
errno = 0;
if (fesetenv(&originalFenv_) != 0) {
std::fprintf(stderr, "fesetenv() failed: %s\n", std::strerror(errno));
std::fprintf(
stderr, "fesetenv() failed: %s\n", llvm::sys::StrError(errno).c_str());
std::abort();
}
}
Expand Down
19 changes: 10 additions & 9 deletions flang/unittests/Evaluate/intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include "flang/Evaluate/expression.h"
#include "flang/Evaluate/tools.h"
#include "flang/Parser/provenance.h"
#include "llvm/Support/raw_ostream.h"
#include <initializer_list>
#include <iostream>
#include <map>
#include <string>

Expand All @@ -31,7 +31,7 @@ class CookedStrings {
parser::ContextualMessages Messages(parser::Messages &buffer) {
return parser::ContextualMessages{cooked_.data(), &buffer};
}
void Emit(std::ostream &o, const parser::Messages &messages) {
void Emit(llvm::raw_ostream &o, const parser::Messages &messages) {
messages.Emit(o, cooked_);
}

Expand Down Expand Up @@ -88,17 +88,18 @@ struct TestCall {
int rank = 0, bool isElemental = false) {
Marshal();
parser::CharBlock fName{strings(name)};
std::cout << "function: " << fName.ToString();
llvm::outs() << "function: " << fName.ToString();
char sep{'('};
for (const auto &a : args) {
std::cout << sep;
llvm::outs() << sep;
sep = ',';
a->AsFortran(std::cout);
a->AsFortran(llvm::outs());
}
if (sep == '(') {
std::cout << '(';
llvm::outs() << '(';
}
std::cout << ')' << std::endl;
llvm::outs() << ')' << '\n';
llvm::outs().flush();
CallCharacteristics call{fName.ToString()};
auto messages{strings.Messages(buffer)};
FoldingContext context{messages, defaults, table};
Expand Down Expand Up @@ -126,7 +127,7 @@ struct TestCall {
TEST((messages.messages() && messages.messages()->AnyFatalError()) ||
name == "bad");
}
strings.Emit(std::cout, buffer);
strings.Emit(llvm::outs(), buffer);
}

const common::IntrinsicTypeDefaultKinds &defaults;
Expand All @@ -143,7 +144,7 @@ void TestIntrinsics() {
MATCH(4, defaults.GetDefaultKind(TypeCategory::Integer));
MATCH(4, defaults.GetDefaultKind(TypeCategory::Real));
IntrinsicProcTable table{IntrinsicProcTable::Configure(defaults)};
table.Dump(std::cout);
table.Dump(llvm::outs());

using Int1 = Type<TypeCategory::Integer, 1>;
using Int4 = Type<TypeCategory::Integer, 4>;
Expand Down
9 changes: 6 additions & 3 deletions flang/unittests/Evaluate/real.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "fp-testing.h"
#include "testing.h"
#include "flang/Evaluate/type.h"
#include "llvm/Support/raw_ostream.h"
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <type_traits>

using namespace Fortran::evaluate;
Expand Down Expand Up @@ -149,7 +149,8 @@ template<typename R> void basicTests(int rm, Rounding rounding) {
TEST(ivf.flags.empty())(ldesc);
MATCH(x, ivf.value.ToUInt64())(ldesc);
if (rounding.mode == RoundingMode::TiesToEven) { // to match stold()
std::stringstream ss;
std::string buf;
llvm::raw_string_ostream ss{buf};
vr.value.AsFortran(ss, kind, false /*exact*/);
std::string decimal{ss.str()};
const char *p{decimal.data()};
Expand Down Expand Up @@ -398,7 +399,9 @@ void subsetTests(int pass, Rounding rounding, std::uint32_t opds) {
("%d IsInfinite(0x%jx)", pass, static_cast<std::intmax_t>(rj));

static constexpr int kind{REAL::bits / 8};
std::stringstream ss, css;
std::string ssBuf, cssBuf;
llvm::raw_string_ostream ss{ssBuf};
llvm::raw_string_ostream css{cssBuf};
x.AsFortran(ss, kind, false /*exact*/);
std::string s{ss.str()};
if (IsNaN(rj)) {
Expand Down
14 changes: 7 additions & 7 deletions flang/unittests/Evaluate/testing.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "testing.h"
#include "llvm/Support/raw_ostream.h"
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <iostream>

namespace testing {

Expand Down Expand Up @@ -103,22 +103,22 @@ FailureDetailPrinter Compare(const char *file, int line, const char *xs,
int Complete() {
if (failures == 0) {
if (passes == 1) {
std::cout << "single test PASSES\n";
llvm::outs() << "single test PASSES\n";
} else {
std::cout << "all " << std::dec << passes << " tests PASS\n";
llvm::outs() << "all " << passes << " tests PASS\n";
}
passes = 0;
return EXIT_SUCCESS;
} else {
if (passes == 1) {
std::cerr << "1 test passes, ";
llvm::errs() << "1 test passes, ";
} else {
std::cerr << std::dec << passes << " tests pass, ";
llvm::errs() << passes << " tests pass, ";
}
if (failures == 1) {
std::cerr << "1 test FAILS\n";
llvm::errs() << "1 test FAILS\n";
} else {
std::cerr << std::dec << failures << " tests FAIL\n";
llvm::errs() << failures << " tests FAIL\n";
}
passes = failures = 0;
return EXIT_FAILURE;
Expand Down
6 changes: 3 additions & 3 deletions flang/unittests/Evaluate/uint128.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#define AVOID_NATIVE_UINT128_T 1
#include "flang/Common/uint128.h"
#include "testing.h"
#include "llvm/Support/raw_ostream.h"
#include <cinttypes>
#include <iostream>

#if (defined __GNUC__ || defined __clang__) && defined __SIZEOF_INT128__
#define HAS_NATIVE_UINT128_T 1
Expand Down Expand Up @@ -123,10 +123,10 @@ int main() {
}
}
#if HAS_NATIVE_UINT128_T
std::cout << "Environment has native __uint128_t\n";
llvm::outs() << "Environment has native __uint128_t\n";
TestVsNative();
#else
std::cout << "Environment lacks native __uint128_t\n";
llvm::outs() << "Environment lacks native __uint128_t\n";
#endif
return testing::Complete();
}
4 changes: 4 additions & 0 deletions flang/unittests/Runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ add_executable(format-test
target_link_libraries(format-test
FortranRuntime
RuntimeTesting
LLVMSupport
)

add_test(Format format-test)
Expand All @@ -32,6 +33,7 @@ add_executable(hello-world
target_link_libraries(hello-world
FortranRuntime
RuntimeTesting
LLVMSupport
)

add_test(HelloWorld hello-world)
Expand All @@ -42,6 +44,7 @@ add_executable(external-hello-world

target_link_libraries(external-hello-world
FortranRuntime
LLVMSupport
)

add_executable(list-input-test
Expand All @@ -51,6 +54,7 @@ add_executable(list-input-test
target_link_libraries(list-input-test
FortranRuntime
RuntimeTesting
LLVMSupport
)

add_test(ListInput list-input-test)
10 changes: 5 additions & 5 deletions flang/unittests/Runtime/format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include "testing.h"
#include "../runtime/format-implementation.h"
#include "../runtime/io-error.h"
#include "llvm/Support/raw_ostream.h"
#include <cstdarg>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

Expand Down Expand Up @@ -93,13 +93,13 @@ void TestFormatContext::Check(Results &expect) {
if (expect != results) {
Fail() << "expected:";
for (const std::string &s : expect) {
std::cerr << ' ' << s;
llvm::errs() << ' ' << s;
}
std::cerr << "\ngot:";
llvm::errs() << "\ngot:";
for (const std::string &s : results) {
std::cerr << ' ' << s;
llvm::errs() << ' ' << s;
}
std::cerr << '\n';
llvm::errs() << '\n';
}
expect.clear();
results.clear();
Expand Down
6 changes: 3 additions & 3 deletions flang/unittests/Runtime/hello.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include "testing.h"
#include "../../runtime/descriptor.h"
#include "../../runtime/io-api.h"
#include "llvm/Support/raw_ostream.h"
#include <cstring>
#include <iostream>

using namespace Fortran::runtime;
using namespace Fortran::runtime::io;
Expand Down Expand Up @@ -110,8 +110,8 @@ static void realInTest(
Fail() << '\'' << format << "' failed reading '" << data << "', status "
<< static_cast<int>(status) << " iomsg '" << iomsg << "'\n";
} else if (u.raw != want) {
Fail() << '\'' << format << "' failed reading '" << data << "', want 0x"
<< std::hex << want << ", got 0x" << u.raw << std::dec << '\n';
Fail() << '\'' << format << "' failed reading '" << data << "', want 0x";
Fail().write_hex(want) << ", got 0x" << u.raw << '\n';
}
}

Expand Down
2 changes: 1 addition & 1 deletion flang/unittests/Runtime/list-input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#include "../../runtime/descriptor.h"
#include "../../runtime/io-api.h"
#include "../../runtime/io-error.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cstring>
#include <iostream>

using namespace Fortran::runtime;
using namespace Fortran::runtime::io;
Expand Down
10 changes: 5 additions & 5 deletions flang/unittests/Runtime/testing.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "testing.h"
#include "../../runtime/terminator.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cstdarg>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>

static int failures{0};
Expand All @@ -21,16 +21,16 @@ void StartTests() {
Fortran::runtime::Terminator::RegisterCrashHandler(CatchCrash);
}

std::ostream &Fail() {
llvm::raw_ostream &Fail() {
++failures;
return std::cerr;
return llvm::errs();
}

int EndTests() {
if (failures == 0) {
std::cout << "PASS\n";
llvm::outs() << "PASS\n";
} else {
std::cout << "FAIL " << failures << " tests\n";
llvm::outs() << "FAIL " << failures << " tests\n";
}
return failures != 0;
}
Expand Down
6 changes: 5 additions & 1 deletion flang/unittests/Runtime/testing.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
#include <cstddef>
#include <iosfwd>

namespace llvm {
class raw_ostream;
}

void StartTests();
std::ostream &Fail();
llvm::raw_ostream &Fail();
int EndTests();

void SetCharacter(char *, std::size_t, const char *);
Expand Down