[flang] [LLVMify F18] Replace the use std::ostream with LLVM streams llvm::ostream

This patch replaces the occurrence of std::ostream by llvm::raw_ostream.
In  LLVM Coding Standards[1] "All new code should use raw_ostream
instead of ostream".[1]

As a consequence, this patch also replaces the use of:
   std::stringstream by llvm::raw_string_ostream or llvm::raw_ostream*
   std::ofstream by llvm::raw_fd_ostream
   std::endl by '\n' and flush()[2]
   std::cout by llvm::outs()   and
   std::cerr by llvm::errs()

It also replaces  std::strerro by llvm::sys::StrError** , but NOT in  Fortran
runtime libraries

*std::stringstream were replaced by llvm::raw_ostream in all methods that
used std::stringstream as a parameter. Moreover, it removes the pointers to
these streams.

[1]https://llvm.org/docs/CodingStandards.html
[2]https://releases.llvm.org/2.5/docs/CodingStandards.html#ll_avoidendl

Signed-off-by: Caroline Concatto <caroline.concatto@arm.com>

Running clang-format-7

Signed-off-by: Caroline Concatto <caroline.concatto@arm.com>

Removing residue of ostream library

Signed-off-by: Caroline Concatto <caroline.concatto@arm.com>

Original-commit: flang-compiler/f18@a3507d44b8
Reviewed-on: https://github.com/flang-compiler/f18/pull/1047
This commit is contained in:
Caroline Concatto 2020-02-28 15:11:03 +00:00
parent fc23a1bb79
commit 8670e49901
85 changed files with 722 additions and 540 deletions

View File

@ -160,5 +160,5 @@ is built. All of the following parsers consume characters acquired from
### Debugging Parser ### Debugging Parser
Last, a string literal `"..."_debug` denotes a parser that emits the string to Last, a string literal `"..."_debug` denotes a parser that emits the string to
`std::cerr` and succeeds. It is useful for tracing while debugging a parser but should `llvm::errs` and succeeds. It is useful for tracing while debugging a parser but should
obviously not be committed for production code. obviously not be committed for production code.

View File

@ -16,11 +16,11 @@
#include "constexpr-bitset.h" #include "constexpr-bitset.h"
#include "idioms.h" #include "idioms.h"
#include "llvm/Support/raw_ostream.h"
#include <bitset> #include <bitset>
#include <cstddef> #include <cstddef>
#include <initializer_list> #include <initializer_list>
#include <optional> #include <optional>
#include <ostream>
#include <string> #include <string>
#include <type_traits> #include <type_traits>
@ -199,8 +199,8 @@ public:
} }
} }
std::ostream &Dump( llvm::raw_ostream &Dump(
std::ostream &o, std::string EnumToString(enumerationType)) const { llvm::raw_ostream &o, std::string EnumToString(enumerationType)) const {
char sep{'{'}; char sep{'{'};
IterateOverMembers([&](auto e) { IterateOverMembers([&](auto e) {
o << sep << EnumToString(e); o << sep << EnumToString(e);

View File

@ -18,9 +18,12 @@
#include "flang/Parser/char-block.h" #include "flang/Parser/char-block.h"
#include "flang/Semantics/attr.h" #include "flang/Semantics/attr.h"
#include <optional> #include <optional>
#include <ostream>
#include <vector> #include <vector>
namespace llvm {
class raw_ostream;
}
namespace Fortran::semantics { namespace Fortran::semantics {
class Symbol; class Symbol;
} }
@ -60,7 +63,7 @@ public:
bool operator==(const AssumedType &that) const { bool operator==(const AssumedType &that) const {
return &*symbol_ == &*that.symbol_; return &*symbol_ == &*that.symbol_;
} }
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
private: private:
SymbolRef symbol_; SymbolRef symbol_;
@ -101,7 +104,7 @@ public:
std::optional<DynamicType> GetType() const; std::optional<DynamicType> GetType() const;
int Rank() const; int Rank() const;
bool operator==(const ActualArgument &) const; bool operator==(const ActualArgument &) const;
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
std::optional<parser::CharBlock> keyword() const { return keyword_; } std::optional<parser::CharBlock> keyword() const { return keyword_; }
void set_keyword(parser::CharBlock x) { keyword_ = x; } void set_keyword(parser::CharBlock x) { keyword_ = x; }
@ -146,7 +149,7 @@ struct SpecificIntrinsic {
DECLARE_CONSTRUCTORS_AND_ASSIGNMENTS(SpecificIntrinsic) DECLARE_CONSTRUCTORS_AND_ASSIGNMENTS(SpecificIntrinsic)
~SpecificIntrinsic(); ~SpecificIntrinsic();
bool operator==(const SpecificIntrinsic &) const; bool operator==(const SpecificIntrinsic &) const;
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
IntrinsicProcedure name; IntrinsicProcedure name;
bool isRestrictedSpecific{false}; // if true, can only call it, not pass it bool isRestrictedSpecific{false}; // if true, can only call it, not pass it
@ -177,7 +180,7 @@ struct ProcedureDesignator {
int Rank() const; int Rank() const;
bool IsElemental() const; bool IsElemental() const;
std::optional<Expr<SubscriptInteger>> LEN() const; std::optional<Expr<SubscriptInteger>> LEN() const;
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
std::variant<SpecificIntrinsic, SymbolRef, std::variant<SpecificIntrinsic, SymbolRef,
common::CopyableIndirection<Component>> common::CopyableIndirection<Component>>
@ -200,7 +203,7 @@ public:
int Rank() const; int Rank() const;
bool IsElemental() const { return proc_.IsElemental(); } bool IsElemental() const { return proc_.IsElemental(); }
bool operator==(const ProcedureRef &) const; bool operator==(const ProcedureRef &) const;
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
protected: protected:
ProcedureDesignator proc_; ProcedureDesignator proc_;

View File

@ -24,11 +24,14 @@
#include "flang/Parser/char-block.h" #include "flang/Parser/char-block.h"
#include "flang/Semantics/symbol.h" #include "flang/Semantics/symbol.h"
#include <optional> #include <optional>
#include <ostream>
#include <string> #include <string>
#include <variant> #include <variant>
#include <vector> #include <vector>
namespace llvm {
class raw_ostream;
}
namespace Fortran::evaluate { namespace Fortran::evaluate {
class IntrinsicProcTable; class IntrinsicProcTable;
} }
@ -131,7 +134,7 @@ public:
const char *thisIs = "POINTER", const char *thatIs = "TARGET", const char *thisIs = "POINTER", const char *thatIs = "TARGET",
bool isElemental = false) const; bool isElemental = false) const;
std::ostream &Dump(std::ostream &) const; llvm::raw_ostream &Dump(llvm::raw_ostream &) const;
private: private:
void AcquireShape(const semantics::ObjectEntityDetails &); void AcquireShape(const semantics::ObjectEntityDetails &);
@ -160,7 +163,7 @@ struct DummyDataObject {
} }
static std::optional<DummyDataObject> Characterize(const semantics::Symbol &); static std::optional<DummyDataObject> Characterize(const semantics::Symbol &);
bool CanBePassedViaImplicitInterface() const; bool CanBePassedViaImplicitInterface() const;
std::ostream &Dump(std::ostream &) const; llvm::raw_ostream &Dump(llvm::raw_ostream &) const;
TypeAndShape type; TypeAndShape type;
std::vector<Expr<SubscriptInteger>> coshape; std::vector<Expr<SubscriptInteger>> coshape;
common::Intent intent{common::Intent::Default}; common::Intent intent{common::Intent::Default};
@ -177,7 +180,7 @@ struct DummyProcedure {
bool operator!=(const DummyProcedure &that) const { return !(*this == that); } bool operator!=(const DummyProcedure &that) const { return !(*this == that); }
static std::optional<DummyProcedure> Characterize( static std::optional<DummyProcedure> Characterize(
const semantics::Symbol &, const IntrinsicProcTable &); const semantics::Symbol &, const IntrinsicProcTable &);
std::ostream &Dump(std::ostream &) const; llvm::raw_ostream &Dump(llvm::raw_ostream &) const;
CopyableIndirection<Procedure> procedure; CopyableIndirection<Procedure> procedure;
common::Intent intent{common::Intent::Default}; common::Intent intent{common::Intent::Default};
Attrs attrs; Attrs attrs;
@ -187,7 +190,7 @@ struct DummyProcedure {
struct AlternateReturn { struct AlternateReturn {
bool operator==(const AlternateReturn &) const { return true; } bool operator==(const AlternateReturn &) const { return true; }
bool operator!=(const AlternateReturn &) const { return false; } bool operator!=(const AlternateReturn &) const { return false; }
std::ostream &Dump(std::ostream &) const; llvm::raw_ostream &Dump(llvm::raw_ostream &) const;
}; };
// 15.3.2.1 // 15.3.2.1
@ -208,7 +211,7 @@ struct DummyArgument {
bool IsOptional() const; bool IsOptional() const;
void SetOptional(bool = true); void SetOptional(bool = true);
bool CanBePassedViaImplicitInterface() const; bool CanBePassedViaImplicitInterface() const;
std::ostream &Dump(std::ostream &) const; llvm::raw_ostream &Dump(llvm::raw_ostream &) const;
// name and pass are not characteristics and so does not participate in // name and pass are not characteristics and so does not participate in
// operator== but are needed to determine if procedures are distinguishable // operator== but are needed to determine if procedures are distinguishable
std::string name; std::string name;
@ -247,7 +250,7 @@ struct FunctionResult {
void SetType(DynamicType t) { std::get<TypeAndShape>(u).set_type(t); } void SetType(DynamicType t) { std::get<TypeAndShape>(u).set_type(t); }
bool CanBeReturnedViaImplicitInterface() const; bool CanBeReturnedViaImplicitInterface() const;
std::ostream &Dump(std::ostream &) const; llvm::raw_ostream &Dump(llvm::raw_ostream &) const;
Attrs attrs; Attrs attrs;
std::variant<TypeAndShape, CopyableIndirection<Procedure>> u; std::variant<TypeAndShape, CopyableIndirection<Procedure>> u;
@ -288,7 +291,7 @@ struct Procedure {
int FindPassIndex(std::optional<parser::CharBlock>) const; int FindPassIndex(std::optional<parser::CharBlock>) const;
bool CanBeCalledViaImplicitInterface() const; bool CanBeCalledViaImplicitInterface() const;
bool CanOverride(const Procedure &, std::optional<int> passIndex) const; bool CanOverride(const Procedure &, std::optional<int> passIndex) const;
std::ostream &Dump(std::ostream &) const; llvm::raw_ostream &Dump(llvm::raw_ostream &) const;
std::optional<FunctionResult> functionResult; std::optional<FunctionResult> functionResult;
DummyArguments dummyArguments; DummyArguments dummyArguments;

View File

@ -13,6 +13,10 @@
#include "real.h" #include "real.h"
#include <string> #include <string>
namespace llvm {
class raw_ostream;
}
namespace Fortran::evaluate::value { namespace Fortran::evaluate::value {
template<typename REAL_TYPE> class Complex { template<typename REAL_TYPE> class Complex {
@ -82,7 +86,7 @@ public:
} }
std::string DumpHexadecimal() const; std::string DumpHexadecimal() const;
std::ostream &AsFortran(std::ostream &, int kind) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &, int kind) const;
// TODO: (C)ABS once Real::HYPOT is done // TODO: (C)ABS once Real::HYPOT is done
// TODO: unit testing // TODO: unit testing

View File

@ -14,9 +14,12 @@
#include "flang/Common/default-kinds.h" #include "flang/Common/default-kinds.h"
#include "flang/Common/reference.h" #include "flang/Common/reference.h"
#include <map> #include <map>
#include <ostream>
#include <vector> #include <vector>
namespace llvm {
class raw_ostream;
}
namespace Fortran::semantics { namespace Fortran::semantics {
class Symbol; class Symbol;
} }
@ -110,7 +113,7 @@ public:
constexpr Result result() const { return result_; } constexpr Result result() const { return result_; }
constexpr DynamicType GetType() const { return result_.GetType(); } constexpr DynamicType GetType() const { return result_.GetType(); }
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
protected: protected:
std::vector<Element> Reshape(const ConstantSubscripts &) const; std::vector<Element> Reshape(const ConstantSubscripts &) const;
@ -178,7 +181,7 @@ public:
Scalar<Result> At(const ConstantSubscripts &) const; Scalar<Result> At(const ConstantSubscripts &) const;
Constant Reshape(ConstantSubscripts &&) const; Constant Reshape(ConstantSubscripts &&) const;
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
static constexpr DynamicType GetType() { static constexpr DynamicType GetType() {
return {TypeCategory::Character, KIND}; return {TypeCategory::Character, KIND};
} }

View File

@ -28,11 +28,14 @@
#include "flang/Parser/char-block.h" #include "flang/Parser/char-block.h"
#include <algorithm> #include <algorithm>
#include <list> #include <list>
#include <ostream>
#include <tuple> #include <tuple>
#include <type_traits> #include <type_traits>
#include <variant> #include <variant>
namespace llvm {
class raw_ostream;
}
namespace Fortran::evaluate { namespace Fortran::evaluate {
using common::LogicalOperator; using common::LogicalOperator;
@ -90,7 +93,7 @@ public:
std::optional<DynamicType> GetType() const; std::optional<DynamicType> GetType() const;
int Rank() const; int Rank() const;
std::string AsFortran() const; std::string AsFortran() const;
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
static Derived Rewrite(FoldingContext &, Derived &&); static Derived Rewrite(FoldingContext &, Derived &&);
}; };
@ -185,7 +188,7 @@ public:
return operand_ == that.operand_; return operand_ == that.operand_;
} }
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
private: private:
Container operand_; Container operand_;
@ -212,7 +215,7 @@ struct Convert : public Operation<Convert<TO, FROMCAT>, TO, SomeKind<FROMCAT>> {
using Operand = SomeKind<FROMCAT>; using Operand = SomeKind<FROMCAT>;
using Base = Operation<Convert, Result, Operand>; using Base = Operation<Convert, Result, Operand>;
using Base::Base; using Base::Base;
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
}; };
template<typename A> template<typename A>
@ -446,7 +449,7 @@ public:
template<typename T> explicit ArrayConstructor(const Expr<T> &) {} template<typename T> explicit ArrayConstructor(const Expr<T> &) {}
static constexpr Result result() { return Result{}; } static constexpr Result result() { return Result{}; }
static constexpr DynamicType GetType() { return Result::GetType(); } static constexpr DynamicType GetType() { return Result::GetType(); }
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
}; };
template<int KIND> template<int KIND>
@ -464,7 +467,7 @@ public:
bool operator==(const ArrayConstructor &) const; bool operator==(const ArrayConstructor &) const;
static constexpr Result result() { return Result{}; } static constexpr Result result() { return Result{}; }
static constexpr DynamicType GetType() { return Result::GetType(); } static constexpr DynamicType GetType() { return Result::GetType(); }
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
const Expr<SubscriptInteger> &LEN() const { return length_.value(); } const Expr<SubscriptInteger> &LEN() const { return length_.value(); }
private: private:
@ -488,7 +491,7 @@ public:
bool operator==(const ArrayConstructor &) const; bool operator==(const ArrayConstructor &) const;
constexpr Result result() const { return result_; } constexpr Result result() const { return result_; }
constexpr DynamicType GetType() const { return result_.GetType(); } constexpr DynamicType GetType() const { return result_.GetType(); }
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
private: private:
Result result_; Result result_;
@ -629,7 +632,7 @@ public:
int Rank() const { int Rank() const {
return std::visit([](const auto &x) { return x.Rank(); }, u); return std::visit([](const auto &x) { return x.Rank(); }, u);
} }
std::ostream &AsFortran(std::ostream &o) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &o) const;
common::MapTemplate<Relational, DirectlyComparableTypes> u; common::MapTemplate<Relational, DirectlyComparableTypes> u;
}; };
@ -715,7 +718,7 @@ public:
StructureConstructor &Add(const semantics::Symbol &, Expr<SomeType> &&); StructureConstructor &Add(const semantics::Symbol &, Expr<SomeType> &&);
int Rank() const { return 0; } int Rank() const { return 0; }
DynamicType GetType() const; DynamicType GetType() const;
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
private: private:
Result result_; Result result_;
@ -823,7 +826,7 @@ public:
using BoundsSpec = std::vector<Expr<SubscriptInteger>>; using BoundsSpec = std::vector<Expr<SubscriptInteger>>;
using BoundsRemapping = using BoundsRemapping =
std::vector<std::pair<Expr<SubscriptInteger>, Expr<SubscriptInteger>>>; std::vector<std::pair<Expr<SubscriptInteger>, Expr<SubscriptInteger>>>;
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
Expr<SomeType> lhs; Expr<SomeType> lhs;
Expr<SomeType> rhs; Expr<SomeType> rhs;

View File

@ -9,10 +9,10 @@
#ifndef FORTRAN_EVALUATE_FORMATTING_H_ #ifndef FORTRAN_EVALUATE_FORMATTING_H_
#define FORTRAN_EVALUATE_FORMATTING_H_ #define FORTRAN_EVALUATE_FORMATTING_H_
// It is inconvenient in C++ to have std::ostream::operator<<() as a direct // It is inconvenient in C++ to have llvm::raw_ostream::operator<<() as a direct
// friend function of a class template with many instantiations, so the // friend function of a class template with many instantiations, so the
// various representational class templates in lib/Evaluate format themselves // various representational class templates in lib/Evaluate format themselves
// via AsFortran(std::ostream &) member functions, which the operator<<() // via AsFortran(llvm::raw_ostream &) member functions, which the operator<<()
// overload below will call. Others have AsFortran() member functions that // overload below will call. Others have AsFortran() member functions that
// return strings. // return strings.
// //
@ -20,31 +20,31 @@
// representational class templates that need it, not by external clients. // representational class templates that need it, not by external clients.
#include "flang/Common/indirection.h" #include "flang/Common/indirection.h"
#include "llvm/Support/raw_ostream.h"
#include <optional> #include <optional>
#include <ostream>
#include <type_traits> #include <type_traits>
namespace Fortran::evaluate { namespace Fortran::evaluate {
template<typename A> template<typename A>
auto operator<<(std::ostream &o, const A &x) -> decltype(x.AsFortran(o)) { auto operator<<(llvm::raw_ostream &o, const A &x) -> decltype(x.AsFortran(o)) {
return x.AsFortran(o); return x.AsFortran(o);
} }
template<typename A> template<typename A>
auto operator<<(std::ostream &o, const A &x) -> decltype(o << x.AsFortran()) { auto operator<<(llvm::raw_ostream &o, const A &x) -> decltype(o << x.AsFortran()) {
return o << x.AsFortran(); return o << x.AsFortran();
} }
template<typename A, bool COPYABLE> template<typename A, bool COPYABLE>
auto operator<<( auto operator<<(
std::ostream &o, const Fortran::common::Indirection<A, COPYABLE> &x) llvm::raw_ostream &o, const Fortran::common::Indirection<A, COPYABLE> &x)
-> decltype(o << x.value()) { -> decltype(o << x.value()) {
return o << x.value(); return o << x.value();
} }
template<typename A> template<typename A>
auto operator<<(std::ostream &o, const std::optional<A> &x) auto operator<<(llvm::raw_ostream &o, const std::optional<A> &x)
-> decltype(o << *x) { -> decltype(o << *x) {
if (x) { if (x) {
o << *x; o << *x;

View File

@ -16,9 +16,12 @@
#include "flang/Parser/char-block.h" #include "flang/Parser/char-block.h"
#include "flang/Parser/message.h" #include "flang/Parser/message.h"
#include <optional> #include <optional>
#include <ostream>
#include <string> #include <string>
namespace llvm {
class raw_ostream;
}
namespace Fortran::evaluate { namespace Fortran::evaluate {
class FoldingContext; class FoldingContext;
@ -76,7 +79,7 @@ public:
std::optional<SpecificIntrinsicFunctionInterface> IsSpecificIntrinsicFunction( std::optional<SpecificIntrinsicFunctionInterface> IsSpecificIntrinsicFunction(
const std::string &) const; const std::string &) const;
std::ostream &Dump(std::ostream &) const; llvm::raw_ostream &Dump(llvm::raw_ostream &) const;
private: private:
Implementation *impl_{nullptr}; // owning pointer Implementation *impl_{nullptr}; // owning pointer

View File

@ -16,13 +16,15 @@
#include "flang/Evaluate/common.h" #include "flang/Evaluate/common.h"
#include <cinttypes> #include <cinttypes>
#include <limits> #include <limits>
#include <ostream>
#include <string> #include <string>
// Some environments, viz. clang on Darwin, allow the macro HUGE // Some environments, viz. clang on Darwin, allow the macro HUGE
// to leak out of <math.h> even when it is never directly included. // to leak out of <math.h> even when it is never directly included.
#undef HUGE #undef HUGE
namespace llvm {
class raw_ostream;
}
namespace Fortran::evaluate::value { namespace Fortran::evaluate::value {
// LOG10(2.)*1E12 // LOG10(2.)*1E12
@ -310,7 +312,7 @@ public:
// Emits a character representation for an equivalent Fortran constant // Emits a character representation for an equivalent Fortran constant
// or parenthesized constant expression that produces this value. // or parenthesized constant expression that produces this value.
std::ostream &AsFortran(std::ostream &, int kind, bool minimal = false) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &, int kind, bool minimal = false) const;
private: private:
using Significand = Integer<significandBits>; // no implicit bit using Significand = Integer<significandBits>; // no implicit bit

View File

@ -17,10 +17,13 @@
#include <cinttypes> #include <cinttypes>
#include <memory> #include <memory>
#include <optional> #include <optional>
#include <ostream>
#include <string> #include <string>
#include <vector> #include <vector>
namespace llvm {
class raw_ostream;
}
namespace Fortran::evaluate { namespace Fortran::evaluate {
class StaticDataObject { class StaticDataObject {
@ -63,7 +66,7 @@ public:
std::optional<std::string> AsString() const; std::optional<std::string> AsString() const;
std::optional<std::u16string> AsU16String() const; std::optional<std::u16string> AsU16String() const;
std::optional<std::u32string> AsU32String() const; std::optional<std::u32string> AsU32String() const;
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
static bool bigEndian; static bool bigEndian;

View File

@ -25,10 +25,13 @@
#include "flang/Common/template.h" #include "flang/Common/template.h"
#include "flang/Parser/char-block.h" #include "flang/Parser/char-block.h"
#include <optional> #include <optional>
#include <ostream>
#include <variant> #include <variant>
#include <vector> #include <vector>
namespace llvm {
class raw_ostream;
}
namespace Fortran::semantics { namespace Fortran::semantics {
class Symbol; class Symbol;
} }
@ -49,7 +52,7 @@ struct BaseObject {
EVALUATE_UNION_CLASS_BOILERPLATE(BaseObject) EVALUATE_UNION_CLASS_BOILERPLATE(BaseObject)
int Rank() const; int Rank() const;
std::optional<Expr<SubscriptInteger>> LEN() const; std::optional<Expr<SubscriptInteger>> LEN() const;
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
const Symbol *symbol() const { const Symbol *symbol() const {
if (const auto *result{std::get_if<SymbolRef>(&u)}) { if (const auto *result{std::get_if<SymbolRef>(&u)}) {
return &result->get(); return &result->get();
@ -82,7 +85,7 @@ public:
const Symbol &GetLastSymbol() const { return symbol_; } const Symbol &GetLastSymbol() const { return symbol_; }
std::optional<Expr<SubscriptInteger>> LEN() const; std::optional<Expr<SubscriptInteger>> LEN() const;
bool operator==(const Component &) const; bool operator==(const Component &) const;
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
private: private:
common::CopyableIndirection<DataRef> base_; common::CopyableIndirection<DataRef> base_;
@ -110,7 +113,7 @@ public:
int Rank() const; int Rank() const;
std::optional<Expr<SubscriptInteger>> LEN() const; std::optional<Expr<SubscriptInteger>> LEN() const;
bool operator==(const NamedEntity &) const; bool operator==(const NamedEntity &) const;
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
private: private:
std::variant<SymbolRef, Component> u_; std::variant<SymbolRef, Component> u_;
@ -140,7 +143,7 @@ public:
static constexpr int Rank() { return 0; } // always scalar static constexpr int Rank() { return 0; } // always scalar
bool operator==(const TypeParamInquiry &) const; bool operator==(const TypeParamInquiry &) const;
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
private: private:
std::optional<NamedEntity> base_; std::optional<NamedEntity> base_;
@ -168,7 +171,7 @@ public:
bool operator==(const Triplet &) const; bool operator==(const Triplet &) const;
bool IsStrideOne() const; bool IsStrideOne() const;
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
private: private:
std::optional<IndirectSubscriptIntegerExpr> lower_, upper_; std::optional<IndirectSubscriptIntegerExpr> lower_, upper_;
@ -181,7 +184,7 @@ struct Subscript {
explicit Subscript(Expr<SubscriptInteger> &&s) explicit Subscript(Expr<SubscriptInteger> &&s)
: u{IndirectSubscriptIntegerExpr::Make(std::move(s))} {} : u{IndirectSubscriptIntegerExpr::Make(std::move(s))} {}
int Rank() const; int Rank() const;
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
std::variant<IndirectSubscriptIntegerExpr, Triplet> u; std::variant<IndirectSubscriptIntegerExpr, Triplet> u;
}; };
@ -217,7 +220,7 @@ public:
const Symbol &GetLastSymbol() const; const Symbol &GetLastSymbol() const;
std::optional<Expr<SubscriptInteger>> LEN() const; std::optional<Expr<SubscriptInteger>> LEN() const;
bool operator==(const ArrayRef &) const; bool operator==(const ArrayRef &) const;
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
private: private:
NamedEntity base_; NamedEntity base_;
@ -265,7 +268,7 @@ public:
NamedEntity GetBase() const; NamedEntity GetBase() const;
std::optional<Expr<SubscriptInteger>> LEN() const; std::optional<Expr<SubscriptInteger>> LEN() const;
bool operator==(const CoarrayRef &) const; bool operator==(const CoarrayRef &) const;
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
private: private:
SymbolVector base_; SymbolVector base_;
@ -286,7 +289,7 @@ struct DataRef {
const Symbol &GetFirstSymbol() const; const Symbol &GetFirstSymbol() const;
const Symbol &GetLastSymbol() const; const Symbol &GetLastSymbol() const;
std::optional<Expr<SubscriptInteger>> LEN() const; std::optional<Expr<SubscriptInteger>> LEN() const;
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
std::variant<SymbolRef, Component, ArrayRef, CoarrayRef> u; std::variant<SymbolRef, Component, ArrayRef, CoarrayRef> u;
}; };
@ -327,7 +330,7 @@ public:
const Symbol *GetLastSymbol() const; const Symbol *GetLastSymbol() const;
std::optional<Expr<SubscriptInteger>> LEN() const; std::optional<Expr<SubscriptInteger>> LEN() const;
bool operator==(const Substring &) const; bool operator==(const Substring &) const;
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
std::optional<Expr<SomeCharacter>> Fold(FoldingContext &); std::optional<Expr<SomeCharacter>> Fold(FoldingContext &);
@ -352,7 +355,7 @@ public:
const Symbol &GetFirstSymbol() const { return complex_.GetFirstSymbol(); } const Symbol &GetFirstSymbol() const { return complex_.GetFirstSymbol(); }
const Symbol &GetLastSymbol() const { return complex_.GetLastSymbol(); } const Symbol &GetLastSymbol() const { return complex_.GetLastSymbol(); }
bool operator==(const ComplexPart &) const; bool operator==(const ComplexPart &) const;
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
private: private:
DataRef complex_; DataRef complex_;
@ -387,7 +390,7 @@ public:
BaseObject GetBaseObject() const; BaseObject GetBaseObject() const;
const Symbol *GetLastSymbol() const; const Symbol *GetLastSymbol() const;
std::optional<Expr<SubscriptInteger>> LEN() const; std::optional<Expr<SubscriptInteger>> LEN() const;
std::ostream &AsFortran(std::ostream &o) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &o) const;
Variant u; Variant u;
}; };
@ -405,7 +408,7 @@ template<typename T> struct Variable {
int Rank() const { int Rank() const {
return std::visit([](const auto &x) { return x.Rank(); }, u); return std::visit([](const auto &x) { return x.Rank(); }, u);
} }
std::ostream &AsFortran(std::ostream &o) const { llvm::raw_ostream &AsFortran(llvm::raw_ostream &o) const {
std::visit([&](const auto &x) { x.AsFortran(o); }, u); std::visit([&](const auto &x) { x.AsFortran(o); }, u);
return o; return o;
} }
@ -428,7 +431,7 @@ public:
static constexpr int Rank() { return 0; } // always scalar static constexpr int Rank() { return 0; } // always scalar
bool operator==(const DescriptorInquiry &) const; bool operator==(const DescriptorInquiry &) const;
std::ostream &AsFortran(std::ostream &) const; llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
private: private:
NamedEntity base_; NamedEntity base_;

View File

@ -11,7 +11,6 @@
#include "flang/Common/template.h" #include "flang/Common/template.h"
#include "flang/Parser/parse-tree.h" #include "flang/Parser/parse-tree.h"
#include "llvm/Support/raw_ostream.h"
#include <memory> #include <memory>
/// Build a light-weight tree over the parse-tree to help with lowering to FIR. /// Build a light-weight tree over the parse-tree to help with lowering to FIR.
@ -26,6 +25,10 @@
/// are either statements or constructs, where a construct contains a list of /// are either statements or constructs, where a construct contains a list of
/// evaluations. The resulting PFT structure can then be used to create FIR. /// evaluations. The resulting PFT structure can then be used to create FIR.
namespace llvm {
class raw_ostream;
}
namespace Fortran::lower { namespace Fortran::lower {
namespace pft { namespace pft {

View File

@ -19,6 +19,10 @@
#include <string> #include <string>
#include <utility> #include <utility>
namespace llvm {
class raw_ostream;
}
namespace Fortran::parser { namespace Fortran::parser {
class CharBlock { class CharBlock {
@ -134,7 +138,7 @@ inline bool operator>(const char *left, const CharBlock &right) {
return right < left; return right < left;
} }
std::ostream &operator<<(std::ostream &os, const CharBlock &x); llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const CharBlock &x);
} }

View File

@ -15,8 +15,7 @@
#include "unparse.h" #include "unparse.h"
#include "flang/Common/idioms.h" #include "flang/Common/idioms.h"
#include "flang/Common/indirection.h" #include "flang/Common/indirection.h"
#include <ostream> #include "llvm/Support/raw_ostream.h"
#include <sstream>
#include <string> #include <string>
#include <type_traits> #include <type_traits>
@ -37,7 +36,7 @@ struct HasSource<T, decltype((void)T::source, 0)> : std::true_type {};
class ParseTreeDumper { class ParseTreeDumper {
public: public:
explicit ParseTreeDumper( explicit ParseTreeDumper(
std::ostream &out, const AnalyzedObjectsAsFortran *asFortran = nullptr) llvm::raw_ostream &out, const AnalyzedObjectsAsFortran *asFortran = nullptr)
: out_(out), asFortran_{asFortran} {} : out_(out), asFortran_{asFortran} {}
static constexpr const char *GetNodeName(const char *) { return "char *"; } static constexpr const char *GetNodeName(const char *) { return "char *"; }
@ -761,7 +760,8 @@ public:
protected: protected:
// Return a Fortran representation of this node to include in the dump // Return a Fortran representation of this node to include in the dump
template<typename T> std::string AsFortran(const T &x) { template<typename T> std::string AsFortran(const T &x) {
std::ostringstream ss; std::string buf;
llvm::raw_string_ostream ss{buf};
if constexpr (std::is_same_v<T, Expr>) { if constexpr (std::is_same_v<T, Expr>) {
if (asFortran_ && x.typedExpr) { if (asFortran_ && x.typedExpr) {
asFortran_->expr(ss, *x.typedExpr); asFortran_->expr(ss, *x.typedExpr);
@ -784,7 +784,7 @@ protected:
std::is_same_v<T, std::int64_t> || std::is_same_v<T, std::uint64_t>) { std::is_same_v<T, std::int64_t> || std::is_same_v<T, std::uint64_t>) {
ss << x; ss << x;
} }
if (ss.tellp()) { if (ss.tell()) {
return ss.str(); return ss.str();
} }
if constexpr (std::is_same_v<T, Name> || HasSource<T>::value) { if constexpr (std::is_same_v<T, Name> || HasSource<T>::value) {
@ -830,13 +830,13 @@ protected:
private: private:
int indent_{0}; int indent_{0};
std::ostream &out_; llvm::raw_ostream &out_;
const AnalyzedObjectsAsFortran *const asFortran_; const AnalyzedObjectsAsFortran *const asFortran_;
bool emptyline_{false}; bool emptyline_{false};
}; };
template<typename T> template<typename T>
void DumpTree(std::ostream &out, const T &x, void DumpTree(llvm::raw_ostream &out, const T &x,
const AnalyzedObjectsAsFortran *asFortran = nullptr) { const AnalyzedObjectsAsFortran *asFortran = nullptr) {
ParseTreeDumper dumper{out, asFortran}; ParseTreeDumper dumper{out, asFortran};
Walk(x, dumper); Walk(x, dumper);

View File

@ -15,7 +15,10 @@
#include "flang/Parser/provenance.h" #include "flang/Parser/provenance.h"
#include <cstddef> #include <cstddef>
#include <map> #include <map>
#include <ostream>
namespace llvm {
class raw_ostream;
}
namespace Fortran::parser { namespace Fortran::parser {
@ -28,7 +31,7 @@ public:
bool Fails(const char *at, const MessageFixedText &tag, ParseState &); bool Fails(const char *at, const MessageFixedText &tag, ParseState &);
void Note(const char *at, const MessageFixedText &tag, bool pass, void Note(const char *at, const MessageFixedText &tag, bool pass,
const ParseState &); const ParseState &);
void Dump(std::ostream &, const CookedSource &) const; void Dump(llvm::raw_ostream &, const CookedSource &) const;
private: private:
struct LogForPosition { struct LogForPosition {

View File

@ -22,7 +22,6 @@
#include <cstring> #include <cstring>
#include <forward_list> #include <forward_list>
#include <optional> #include <optional>
#include <ostream>
#include <string> #include <string>
#include <utility> #include <utility>
#include <variant> #include <variant>
@ -187,7 +186,7 @@ public:
std::string ToString() const; std::string ToString() const;
std::optional<ProvenanceRange> GetProvenanceRange(const CookedSource &) const; std::optional<ProvenanceRange> GetProvenanceRange(const CookedSource &) const;
void Emit( void Emit(
std::ostream &, const CookedSource &, bool echoSourceLine = true) const; llvm::raw_ostream &, const CookedSource &, bool echoSourceLine = true) const;
// If this Message or any of its attachments locates itself via a CharBlock // If this Message or any of its attachments locates itself via a CharBlock
// within a particular CookedSource, replace its location with the // within a particular CookedSource, replace its location with the
@ -255,7 +254,7 @@ public:
void Merge(Messages &&); void Merge(Messages &&);
void Copy(const Messages &); void Copy(const Messages &);
void ResolveProvenances(const CookedSource &); void ResolveProvenances(const CookedSource &);
void Emit(std::ostream &, const CookedSource &cooked, void Emit(llvm::raw_ostream &, const CookedSource &cooked,
bool echoSourceLines = true) const; bool echoSourceLines = true) const;
void AttachTo(Message &); void AttachTo(Message &);
bool AnyFatalError() const; bool AnyFatalError() const;

View File

@ -15,8 +15,8 @@
#include "parse-tree.h" #include "parse-tree.h"
#include "provenance.h" #include "provenance.h"
#include "flang/Common/Fortran-features.h" #include "flang/Common/Fortran-features.h"
#include "llvm/Support/raw_ostream.h"
#include <optional> #include <optional>
#include <ostream>
#include <string> #include <string>
#include <utility> #include <utility>
#include <vector> #include <vector>
@ -50,19 +50,19 @@ public:
std::optional<Program> &parseTree() { return parseTree_; } std::optional<Program> &parseTree() { return parseTree_; }
const SourceFile *Prescan(const std::string &path, Options); const SourceFile *Prescan(const std::string &path, Options);
void DumpCookedChars(std::ostream &) const; void DumpCookedChars(llvm::raw_ostream &) const;
void DumpProvenance(std::ostream &) const; void DumpProvenance(llvm::raw_ostream &) const;
void DumpParsingLog(std::ostream &) const; void DumpParsingLog(llvm::raw_ostream &) const;
void Parse(std::ostream *debugOutput = nullptr); void Parse(llvm::raw_ostream &debugOutput);
void ClearLog(); void ClearLog();
void EmitMessage(std::ostream &o, const char *at, const std::string &message, void EmitMessage(llvm::raw_ostream &o, const char *at, const std::string &message,
bool echoSourceLine = false) const { bool echoSourceLine = false) const {
cooked_.allSources().EmitMessage( cooked_.allSources().EmitMessage(
o, cooked_.GetProvenanceRange(CharBlock(at)), message, echoSourceLine); o, cooked_.GetProvenanceRange(CharBlock(at)), message, echoSourceLine);
} }
bool ForTesting(std::string path, std::ostream &); bool ForTesting(std::string path, llvm::raw_ostream &);
private: private:
Options options_; Options options_;

View File

@ -15,12 +15,11 @@
#include "source.h" #include "source.h"
#include "flang/Common/idioms.h" #include "flang/Common/idioms.h"
#include "flang/Common/interval.h" #include "flang/Common/interval.h"
#include "llvm/Support/raw_ostream.h"
#include <cstddef> #include <cstddef>
#include <map> #include <map>
#include <memory> #include <memory>
#include <optional> #include <optional>
#include <ostream>
#include <sstream>
#include <string> #include <string>
#include <utility> #include <utility>
#include <variant> #include <variant>
@ -91,7 +90,7 @@ public:
bool empty() const { return map_.empty(); } bool empty() const { return map_.empty(); }
void Put(ProvenanceRange, std::size_t offset); void Put(ProvenanceRange, std::size_t offset);
std::optional<std::size_t> Map(ProvenanceRange) const; std::optional<std::size_t> Map(ProvenanceRange) const;
std::ostream &Dump(std::ostream &) const; llvm::raw_ostream &Dump(llvm::raw_ostream &) const;
private: private:
// A comparison function object for use in std::multimap<Compare=>. // A comparison function object for use in std::multimap<Compare=>.
@ -120,7 +119,7 @@ public:
ProvenanceRange Map(std::size_t at) const; ProvenanceRange Map(std::size_t at) const;
void RemoveLastBytes(std::size_t); void RemoveLastBytes(std::size_t);
ProvenanceRangeToOffsetMappings Invert(const AllSources &) const; ProvenanceRangeToOffsetMappings Invert(const AllSources &) const;
std::ostream &Dump(std::ostream &) const; llvm::raw_ostream &Dump(llvm::raw_ostream &) const;
private: private:
struct ContiguousProvenanceMapping { struct ContiguousProvenanceMapping {
@ -150,8 +149,8 @@ public:
void PushSearchPathDirectory(std::string); void PushSearchPathDirectory(std::string);
std::string PopSearchPathDirectory(); std::string PopSearchPathDirectory();
const SourceFile *Open(std::string path, std::stringstream *error); const SourceFile *Open(std::string path, llvm::raw_ostream &error);
const SourceFile *ReadStandardInput(std::stringstream *error); const SourceFile *ReadStandardInput(llvm::raw_ostream &error);
ProvenanceRange AddIncludedFile( ProvenanceRange AddIncludedFile(
const SourceFile &, ProvenanceRange, bool isModule = false); const SourceFile &, ProvenanceRange, bool isModule = false);
@ -163,7 +162,7 @@ public:
bool IsValid(ProvenanceRange range) const { bool IsValid(ProvenanceRange range) const {
return range.size() > 0 && range_.Contains(range); return range.size() > 0 && range_.Contains(range);
} }
void EmitMessage(std::ostream &, const std::optional<ProvenanceRange> &, void EmitMessage(llvm::raw_ostream &, const std::optional<ProvenanceRange> &,
const std::string &message, bool echoSourceLine = false) const; const std::string &message, bool echoSourceLine = false) const;
const SourceFile *GetSourceFile( const SourceFile *GetSourceFile(
Provenance, std::size_t *offset = nullptr) const; Provenance, std::size_t *offset = nullptr) const;
@ -174,7 +173,7 @@ public:
Provenance CompilerInsertionProvenance(char ch); Provenance CompilerInsertionProvenance(char ch);
Provenance CompilerInsertionProvenance(const char *, std::size_t); Provenance CompilerInsertionProvenance(const char *, std::size_t);
ProvenanceRange IntersectionWithSourceFiles(ProvenanceRange) const; ProvenanceRange IntersectionWithSourceFiles(ProvenanceRange) const;
std::ostream &Dump(std::ostream &) const; llvm::raw_ostream &Dump(llvm::raw_ostream &) const;
private: private:
struct Inclusion { struct Inclusion {
@ -260,7 +259,7 @@ public:
void Marshal(); // marshals text into one contiguous block void Marshal(); // marshals text into one contiguous block
void CompileProvenanceRangeToOffsetMappings(); void CompileProvenanceRangeToOffsetMappings();
std::string AcquireData() { return std::move(data_); } std::string AcquireData() { return std::move(data_); }
std::ostream &Dump(std::ostream &) const; llvm::raw_ostream &Dump(llvm::raw_ostream &) const;
private: private:
AllSources &allSources_; AllSources &allSources_;

View File

@ -16,11 +16,14 @@
#include "characters.h" #include "characters.h"
#include <cstddef> #include <cstddef>
#include <sstream>
#include <string> #include <string>
#include <utility> #include <utility>
#include <vector> #include <vector>
namespace llvm {
class raw_ostream;
}
namespace Fortran::parser { namespace Fortran::parser {
std::string DirectoryName(std::string path); std::string DirectoryName(std::string path);
@ -44,8 +47,8 @@ public:
std::size_t lines() const { return lineStart_.size(); } std::size_t lines() const { return lineStart_.size(); }
Encoding encoding() const { return encoding_; } Encoding encoding() const { return encoding_; }
bool Open(std::string path, std::stringstream *error); bool Open(std::string path, llvm::raw_ostream &error);
bool ReadStandardInput(std::stringstream *error); bool ReadStandardInput(llvm::raw_ostream &error);
void Close(); void Close();
SourcePosition FindOffsetLineAndColumn(std::size_t) const; SourcePosition FindOffsetLineAndColumn(std::size_t) const;
std::size_t GetLineStartOffset(int lineNumber) const { std::size_t GetLineStartOffset(int lineNumber) const {
@ -53,7 +56,7 @@ public:
} }
private: private:
bool ReadFile(std::string errorPath, std::stringstream *error); bool ReadFile(std::string errorPath, llvm::raw_ostream &error);
void IdentifyPayload(); void IdentifyPayload();
void RecordLineStarts(); void RecordLineStarts();

View File

@ -14,6 +14,10 @@
#include <functional> #include <functional>
#include <iosfwd> #include <iosfwd>
namespace llvm {
class raw_ostream;
}
namespace Fortran::evaluate { namespace Fortran::evaluate {
struct GenericExprWrapper; struct GenericExprWrapper;
struct GenericAssignmentWrapper; struct GenericAssignmentWrapper;
@ -26,21 +30,21 @@ struct Program;
// A function called before each Statement is unparsed. // A function called before each Statement is unparsed.
using preStatementType = using preStatementType =
std::function<void(const CharBlock &, std::ostream &, int)>; std::function<void(const CharBlock &, llvm::raw_ostream &, int)>;
// Functions to handle unparsing of analyzed expressions and related // Functions to handle unparsing of analyzed expressions and related
// objects rather than their original parse trees. // objects rather than their original parse trees.
struct AnalyzedObjectsAsFortran { struct AnalyzedObjectsAsFortran {
std::function<void(std::ostream &, const evaluate::GenericExprWrapper &)> std::function<void(llvm::raw_ostream &, const evaluate::GenericExprWrapper &)>
expr; expr;
std::function<void( std::function<void(
std::ostream &, const evaluate::GenericAssignmentWrapper &)> llvm::raw_ostream &, const evaluate::GenericAssignmentWrapper &)>
assignment; assignment;
std::function<void(std::ostream &, const evaluate::ProcedureRef &)> call; std::function<void(llvm::raw_ostream &, const evaluate::ProcedureRef &)> call;
}; };
// Converts parsed program to out as Fortran. // Converts parsed program to out as Fortran.
void Unparse(std::ostream &out, const Program &program, void Unparse(llvm::raw_ostream &out, const Program &program,
Encoding encoding = Encoding::UTF_8, bool capitalizeKeywords = true, Encoding encoding = Encoding::UTF_8, bool capitalizeKeywords = true,
bool backslashEscapes = true, preStatementType *preStatement = nullptr, bool backslashEscapes = true, preStatementType *preStatement = nullptr,
AnalyzedObjectsAsFortran * = nullptr); AnalyzedObjectsAsFortran * = nullptr);

View File

@ -18,9 +18,9 @@
#include "flang/Common/idioms.h" #include "flang/Common/idioms.h"
#include "flang/Parser/char-block.h" #include "flang/Parser/char-block.h"
#include "flang/Parser/parse-tree.h" #include "flang/Parser/parse-tree.h"
#include "llvm/Support/raw_ostream.h"
#include <cinttypes> #include <cinttypes>
#include <optional> #include <optional>
#include <ostream>
#include <set> #include <set>
#include <unordered_map> #include <unordered_map>
@ -40,9 +40,9 @@ public:
const CookedSource &cooked() const { return cooked_; } const CookedSource &cooked() const { return cooked_; }
const common::LanguageFeatureControl &features() const { return features_; } const common::LanguageFeatureControl &features() const { return features_; }
std::ostream *debugOutput() const { return debugOutput_; } llvm::raw_ostream *debugOutput() const { return debugOutput_; }
UserState &set_debugOutput(std::ostream *out) { UserState &set_debugOutput(llvm::raw_ostream &out) {
debugOutput_ = out; debugOutput_ = &out;
return *this; return *this;
} }
@ -91,7 +91,7 @@ public:
private: private:
const CookedSource &cooked_; const CookedSource &cooked_;
std::ostream *debugOutput_{nullptr}; llvm::raw_ostream *debugOutput_{nullptr};
ParsingLog *log_{nullptr}; ParsingLog *log_{nullptr};
bool instrumentedParse_{false}; bool instrumentedParse_{false};

View File

@ -14,6 +14,10 @@
#include <cinttypes> #include <cinttypes>
#include <string> #include <string>
namespace llvm {
class raw_ostream;
}
namespace Fortran::semantics { namespace Fortran::semantics {
// All available attributes. // All available attributes.
@ -38,13 +42,13 @@ public:
void CheckValid(const Attrs &allowed) const; void CheckValid(const Attrs &allowed) const;
private: private:
friend std::ostream &operator<<(std::ostream &, const Attrs &); friend llvm::raw_ostream &operator<<(llvm::raw_ostream &, const Attrs &);
}; };
// Return string representation of attr that matches Fortran source. // Return string representation of attr that matches Fortran source.
std::string AttrToString(Attr attr); std::string AttrToString(Attr attr);
std::ostream &operator<<(std::ostream &o, Attr attr); llvm::raw_ostream &operator<<(llvm::raw_ostream &o, Attr attr);
std::ostream &operator<<(std::ostream &o, const Attrs &attrs); llvm::raw_ostream &operator<<(llvm::raw_ostream &o, const Attrs &attrs);
} }
#endif // FORTRAN_SEMANTICS_ATTR_H_ #endif // FORTRAN_SEMANTICS_ATTR_H_

View File

@ -22,6 +22,10 @@
#include <set> #include <set>
#include <string> #include <string>
namespace llvm {
class raw_ostream;
}
namespace Fortran::semantics { namespace Fortran::semantics {
using namespace parser::literals; using namespace parser::literals;
@ -234,7 +238,7 @@ private:
bool CanImport(const SourceName &) const; bool CanImport(const SourceName &) const;
const DeclTypeSpec &MakeLengthlessType(DeclTypeSpec &&); const DeclTypeSpec &MakeLengthlessType(DeclTypeSpec &&);
friend std::ostream &operator<<(std::ostream &, const Scope &); friend llvm::raw_ostream &operator<<(llvm::raw_ostream &, const Scope &);
}; };
} }
#endif // FORTRAN_SEMANTICS_SCOPE_H_ #endif // FORTRAN_SEMANTICS_SCOPE_H_

View File

@ -19,6 +19,10 @@
#include <string> #include <string>
#include <vector> #include <vector>
namespace llvm {
class raw_ostream;
}
namespace Fortran::common { namespace Fortran::common {
class IntrinsicTypeDefaultKinds; class IntrinsicTypeDefaultKinds;
} }
@ -209,9 +213,9 @@ public:
return context_.FindScope(where); return context_.FindScope(where);
} }
bool AnyFatalError() const { return context_.AnyFatalError(); } bool AnyFatalError() const { return context_.AnyFatalError(); }
void EmitMessages(std::ostream &) const; void EmitMessages(llvm::raw_ostream &) const;
void DumpSymbols(std::ostream &); void DumpSymbols(llvm::raw_ostream &);
void DumpSymbolsSources(std::ostream &) const; void DumpSymbolsSources(llvm::raw_ostream &) const;
private: private:
SemanticsContext &context_; SemanticsContext &context_;

View File

@ -19,6 +19,10 @@
#include <set> #include <set>
#include <vector> #include <vector>
namespace llvm {
class raw_ostream;
}
namespace Fortran::semantics { namespace Fortran::semantics {
/// A Symbol consists of common information (name, owner, and attributes) /// A Symbol consists of common information (name, owner, and attributes)
@ -79,7 +83,7 @@ private:
std::vector<Symbol *> dummyArgs_; // nullptr -> alternate return indicator std::vector<Symbol *> dummyArgs_; // nullptr -> alternate return indicator
Symbol *result_{nullptr}; Symbol *result_{nullptr};
MaybeExpr stmtFunction_; MaybeExpr stmtFunction_;
friend std::ostream &operator<<(std::ostream &, const SubprogramDetails &); friend llvm::raw_ostream &operator<<(llvm::raw_ostream &, const SubprogramDetails &);
}; };
// For SubprogramNameDetails, the kind indicates whether it is the name // For SubprogramNameDetails, the kind indicates whether it is the name
@ -121,7 +125,7 @@ private:
bool isFuncResult_{false}; bool isFuncResult_{false};
const DeclTypeSpec *type_{nullptr}; const DeclTypeSpec *type_{nullptr};
MaybeExpr bindName_; MaybeExpr bindName_;
friend std::ostream &operator<<(std::ostream &, const EntityDetails &); friend llvm::raw_ostream &operator<<(llvm::raw_ostream &, const EntityDetails &);
}; };
// Symbol is associated with a name or expression in a SELECT TYPE or ASSOCIATE. // Symbol is associated with a name or expression in a SELECT TYPE or ASSOCIATE.
@ -176,7 +180,7 @@ private:
ArraySpec shape_; ArraySpec shape_;
ArraySpec coshape_; ArraySpec coshape_;
const Symbol *commonBlock_{nullptr}; // common block this object is in const Symbol *commonBlock_{nullptr}; // common block this object is in
friend std::ostream &operator<<(std::ostream &, const ObjectEntityDetails &); friend llvm::raw_ostream &operator<<(llvm::raw_ostream &, const ObjectEntityDetails &);
}; };
// Mixin for details with passed-object dummy argument. // Mixin for details with passed-object dummy argument.
@ -213,7 +217,7 @@ public:
private: private:
ProcInterface interface_; ProcInterface interface_;
std::optional<const Symbol *> init_; std::optional<const Symbol *> init_;
friend std::ostream &operator<<(std::ostream &, const ProcEntityDetails &); friend llvm::raw_ostream &operator<<(llvm::raw_ostream &, const ProcEntityDetails &);
}; };
// These derived type details represent the characteristics of a derived // These derived type details represent the characteristics of a derived
@ -259,7 +263,7 @@ private:
std::list<SourceName> componentNames_; std::list<SourceName> componentNames_;
bool sequence_{false}; bool sequence_{false};
bool isForwardReferenced_{false}; bool isForwardReferenced_{false};
friend std::ostream &operator<<(std::ostream &, const DerivedTypeDetails &); friend llvm::raw_ostream &operator<<(llvm::raw_ostream &, const DerivedTypeDetails &);
}; };
class ProcBindingDetails : public WithPassArg { class ProcBindingDetails : public WithPassArg {
@ -443,7 +447,7 @@ using Details = std::variant<UnknownDetails, MainProgramDetails, ModuleDetails,
DerivedTypeDetails, UseDetails, UseErrorDetails, HostAssocDetails, DerivedTypeDetails, UseDetails, UseErrorDetails, HostAssocDetails,
GenericDetails, ProcBindingDetails, NamelistDetails, CommonBlockDetails, GenericDetails, ProcBindingDetails, NamelistDetails, CommonBlockDetails,
FinalProcDetails, TypeParamDetails, MiscDetails>; FinalProcDetails, TypeParamDetails, MiscDetails>;
std::ostream &operator<<(std::ostream &, const Details &); llvm::raw_ostream &operator<<(llvm::raw_ostream &, const Details &);
std::string DetailsToString(const Details &); std::string DetailsToString(const Details &);
class Symbol { class Symbol {
@ -657,8 +661,8 @@ private:
Symbol() {} // only created in class Symbols Symbol() {} // only created in class Symbols
const std::string GetDetailsName() const; const std::string GetDetailsName() const;
friend std::ostream &operator<<(std::ostream &, const Symbol &); friend llvm::raw_ostream &operator<<(llvm::raw_ostream &, const Symbol &);
friend std::ostream &DumpForUnparse(std::ostream &, const Symbol &, bool); friend llvm::raw_ostream &DumpForUnparse(llvm::raw_ostream &, const Symbol &, bool);
// If a derived type's symbol refers to an extended derived type, // If a derived type's symbol refers to an extended derived type,
// return the parent component's symbol. The scope of the derived type // return the parent component's symbol. The scope of the derived type
@ -669,7 +673,7 @@ private:
template<class, std::size_t> friend struct std::array; template<class, std::size_t> friend struct std::array;
}; };
std::ostream &operator<<(std::ostream &, Symbol::Flag); llvm::raw_ostream &operator<<(llvm::raw_ostream &, Symbol::Flag);
// Manage memory for all symbols. BLOCK_SIZE symbols at a time are allocated. // Manage memory for all symbols. BLOCK_SIZE symbols at a time are allocated.
// Make() returns a reference to the next available one. They are never // Make() returns a reference to the next available one. They are never

View File

@ -21,6 +21,10 @@
#include <variant> #include <variant>
#include <vector> #include <vector>
namespace llvm {
class raw_ostream;
}
namespace Fortran::parser { namespace Fortran::parser {
struct Keyword; struct Keyword;
} }
@ -71,7 +75,7 @@ private:
: category_{category}, expr_{std::move(expr)} {} : category_{category}, expr_{std::move(expr)} {}
Category category_{Category::Explicit}; Category category_{Category::Explicit};
MaybeSubscriptIntExpr expr_; MaybeSubscriptIntExpr expr_;
friend std::ostream &operator<<(std::ostream &, const Bound &); friend llvm::raw_ostream &operator<<(llvm::raw_ostream &, const Bound &);
}; };
// A type parameter value: integer expression or assumed or deferred. // A type parameter value: integer expression or assumed or deferred.
@ -107,7 +111,7 @@ private:
Category category_{Category::Explicit}; Category category_{Category::Explicit};
common::TypeParamAttr attr_{common::TypeParamAttr::Kind}; common::TypeParamAttr attr_{common::TypeParamAttr::Kind};
MaybeIntExpr expr_; MaybeIntExpr expr_;
friend std::ostream &operator<<(std::ostream &, const ParamValue &); friend llvm::raw_ostream &operator<<(llvm::raw_ostream &, const ParamValue &);
}; };
class IntrinsicTypeSpec { class IntrinsicTypeSpec {
@ -126,7 +130,7 @@ protected:
private: private:
TypeCategory category_; TypeCategory category_;
KindExpr kind_; KindExpr kind_;
friend std::ostream &operator<<(std::ostream &os, const IntrinsicTypeSpec &x); friend llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const IntrinsicTypeSpec &x);
}; };
class NumericTypeSpec : public IntrinsicTypeSpec { class NumericTypeSpec : public IntrinsicTypeSpec {
@ -153,7 +157,7 @@ public:
private: private:
ParamValue length_; ParamValue length_;
friend std::ostream &operator<<(std::ostream &os, const CharacterTypeSpec &x); friend llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const CharacterTypeSpec &x);
}; };
class ShapeSpec { class ShapeSpec {
@ -205,7 +209,7 @@ private:
ShapeSpec(Bound &&lb, Bound &&ub) : lb_{std::move(lb)}, ub_{std::move(ub)} {} ShapeSpec(Bound &&lb, Bound &&ub) : lb_{std::move(lb)}, ub_{std::move(ub)} {}
Bound lb_; Bound lb_;
Bound ub_; Bound ub_;
friend std::ostream &operator<<(std::ostream &, const ShapeSpec &); friend llvm::raw_ostream &operator<<(llvm::raw_ostream &, const ShapeSpec &);
}; };
struct ArraySpec : public std::vector<ShapeSpec> { struct ArraySpec : public std::vector<ShapeSpec> {
@ -224,7 +228,7 @@ private:
return !empty() && std::all_of(begin(), end(), predicate); return !empty() && std::all_of(begin(), end(), predicate);
} }
}; };
std::ostream &operator<<(std::ostream &, const ArraySpec &); llvm::raw_ostream &operator<<(llvm::raw_ostream &, const ArraySpec &);
// Each DerivedTypeSpec has a typeSymbol that has DerivedTypeDetails. // Each DerivedTypeSpec has a typeSymbol that has DerivedTypeDetails.
// The name may not match the symbol's name in case of a USE rename. // The name may not match the symbol's name in case of a USE rename.
@ -289,7 +293,7 @@ private:
bool instantiated_{false}; bool instantiated_{false};
RawParameters rawParameters_; RawParameters rawParameters_;
ParameterMapType parameters_; ParameterMapType parameters_;
friend std::ostream &operator<<(std::ostream &, const DerivedTypeSpec &); friend llvm::raw_ostream &operator<<(llvm::raw_ostream &, const DerivedTypeSpec &);
}; };
class DeclTypeSpec { class DeclTypeSpec {
@ -370,7 +374,7 @@ private:
CharacterTypeSpec, DerivedTypeSpec> CharacterTypeSpec, DerivedTypeSpec>
typeSpec_; typeSpec_;
}; };
std::ostream &operator<<(std::ostream &, const DeclTypeSpec &); llvm::raw_ostream &operator<<(llvm::raw_ostream &, const DeclTypeSpec &);
// This represents a proc-interface in the declaration of a procedure or // This represents a proc-interface in the declaration of a procedure or
// procedure component. It comprises a symbol that represents the specific // procedure component. It comprises a symbol that represents the specific

View File

@ -12,12 +12,16 @@
#include "flang/Parser/characters.h" #include "flang/Parser/characters.h"
#include <iosfwd> #include <iosfwd>
namespace llvm {
class raw_ostream;
}
namespace Fortran::parser { namespace Fortran::parser {
struct Program; struct Program;
} }
namespace Fortran::semantics { namespace Fortran::semantics {
void UnparseWithSymbols(std::ostream &, const parser::Program &, void UnparseWithSymbols(llvm::raw_ostream &, const parser::Program &,
parser::Encoding encoding = parser::Encoding::UTF_8); parser::Encoding encoding = parser::Encoding::UTF_8);
} }

View File

@ -16,8 +16,8 @@
#include "flang/Parser/message.h" #include "flang/Parser/message.h"
#include "flang/Semantics/scope.h" #include "flang/Semantics/scope.h"
#include "flang/Semantics/symbol.h" #include "flang/Semantics/symbol.h"
#include "llvm/Support/raw_ostream.h"
#include <initializer_list> #include <initializer_list>
#include <ostream>
using namespace Fortran::parser::literals; using namespace Fortran::parser::literals;
@ -179,7 +179,7 @@ void TypeAndShape::AcquireLEN() {
} }
} }
std::ostream &TypeAndShape::Dump(std::ostream &o) const { llvm::raw_ostream &TypeAndShape::Dump(llvm::raw_ostream &o) const {
o << type_.AsFortran(LEN_ ? LEN_->AsFortran() : ""); o << type_.AsFortran(LEN_ ? LEN_->AsFortran() : "");
attrs_.Dump(o, EnumToString); attrs_.Dump(o, EnumToString);
if (!shape_.empty()) { if (!shape_.empty()) {
@ -261,7 +261,7 @@ bool DummyDataObject::CanBePassedViaImplicitInterface() const {
} }
} }
std::ostream &DummyDataObject::Dump(std::ostream &o) const { llvm::raw_ostream &DummyDataObject::Dump(llvm::raw_ostream &o) const {
attrs.Dump(o, EnumToString); attrs.Dump(o, EnumToString);
if (intent != common::Intent::Default) { if (intent != common::Intent::Default) {
o << "INTENT(" << common::EnumToString(intent) << ')'; o << "INTENT(" << common::EnumToString(intent) << ')';
@ -306,7 +306,7 @@ std::optional<DummyProcedure> DummyProcedure::Characterize(
} }
} }
std::ostream &DummyProcedure::Dump(std::ostream &o) const { llvm::raw_ostream &DummyProcedure::Dump(llvm::raw_ostream &o) const {
attrs.Dump(o, EnumToString); attrs.Dump(o, EnumToString);
if (intent != common::Intent::Default) { if (intent != common::Intent::Default) {
o << "INTENT(" << common::EnumToString(intent) << ')'; o << "INTENT(" << common::EnumToString(intent) << ')';
@ -315,7 +315,9 @@ std::ostream &DummyProcedure::Dump(std::ostream &o) const {
return o; return o;
} }
std::ostream &AlternateReturn::Dump(std::ostream &o) const { return o << '*'; } llvm::raw_ostream &AlternateReturn::Dump(llvm::raw_ostream &o) const {
return o << '*';
}
DummyArgument::~DummyArgument() {} DummyArgument::~DummyArgument() {}
@ -417,7 +419,7 @@ bool DummyArgument::CanBePassedViaImplicitInterface() const {
} }
} }
std::ostream &DummyArgument::Dump(std::ostream &o) const { llvm::raw_ostream &DummyArgument::Dump(llvm::raw_ostream &o) const {
if (!name.empty()) { if (!name.empty()) {
o << name << '='; o << name << '=';
} }
@ -503,7 +505,7 @@ bool FunctionResult::CanBeReturnedViaImplicitInterface() const {
} }
} }
std::ostream &FunctionResult::Dump(std::ostream &o) const { llvm::raw_ostream &FunctionResult::Dump(llvm::raw_ostream &o) const {
attrs.Dump(o, EnumToString); attrs.Dump(o, EnumToString);
std::visit( std::visit(
common::visitors{ common::visitors{
@ -698,7 +700,7 @@ bool Procedure::CanBeCalledViaImplicitInterface() const {
} }
} }
std::ostream &Procedure::Dump(std::ostream &o) const { llvm::raw_ostream &Procedure::Dump(llvm::raw_ostream &o) const {
attrs.Dump(o, EnumToString); attrs.Dump(o, EnumToString);
if (functionResult) { if (functionResult) {
functionResult->Dump(o << "TYPE(") << ") FUNCTION"; functionResult->Dump(o << "TYPE(") << ") FUNCTION";

View File

@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "flang/Evaluate/complex.h" #include "flang/Evaluate/complex.h"
#include "llvm/Support/raw_ostream.h"
namespace Fortran::evaluate::value { namespace Fortran::evaluate::value {
@ -90,7 +91,7 @@ template<typename R> std::string Complex<R>::DumpHexadecimal() const {
} }
template<typename R> template<typename R>
std::ostream &Complex<R>::AsFortran(std::ostream &o, int kind) const { llvm::raw_ostream &Complex<R>::AsFortran(llvm::raw_ostream &o, int kind) const {
re_.AsFortran(o << '(', kind); re_.AsFortran(o << '(', kind);
im_.AsFortran(o << ',', kind); im_.AsFortran(o << ',', kind);
return o << ')'; return o << ')';

View File

@ -13,6 +13,7 @@
#include "flang/Evaluate/tools.h" #include "flang/Evaluate/tools.h"
#include "flang/Evaluate/variable.h" #include "flang/Evaluate/variable.h"
#include "flang/Parser/message.h" #include "flang/Parser/message.h"
#include "llvm/Support/raw_ostream.h"
#include <string> #include <string>
#include <type_traits> #include <type_traits>

View File

@ -14,11 +14,12 @@
#include "flang/Evaluate/tools.h" #include "flang/Evaluate/tools.h"
#include "flang/Parser/characters.h" #include "flang/Parser/characters.h"
#include "flang/Semantics/symbol.h" #include "flang/Semantics/symbol.h"
#include <sstream> #include "llvm/Support/raw_ostream.h"
namespace Fortran::evaluate { namespace Fortran::evaluate {
static void ShapeAsFortran(std::ostream &o, const ConstantSubscripts &shape) { static void ShapeAsFortran(
llvm::raw_ostream &o, const ConstantSubscripts &shape) {
if (GetRank(shape) > 1) { if (GetRank(shape) > 1) {
o << ",shape="; o << ",shape=";
char ch{'['}; char ch{'['};
@ -31,7 +32,8 @@ static void ShapeAsFortran(std::ostream &o, const ConstantSubscripts &shape) {
} }
template<typename RESULT, typename VALUE> template<typename RESULT, typename VALUE>
std::ostream &ConstantBase<RESULT, VALUE>::AsFortran(std::ostream &o) const { llvm::raw_ostream &ConstantBase<RESULT, VALUE>::AsFortran(
llvm::raw_ostream &o) const {
if (Rank() > 1) { if (Rank() > 1) {
o << "reshape("; o << "reshape(";
} }
@ -71,8 +73,8 @@ std::ostream &ConstantBase<RESULT, VALUE>::AsFortran(std::ostream &o) const {
} }
template<int KIND> template<int KIND>
std::ostream &Constant<Type<TypeCategory::Character, KIND>>::AsFortran( llvm::raw_ostream &Constant<Type<TypeCategory::Character, KIND>>::AsFortran(
std::ostream &o) const { llvm::raw_ostream &o) const {
if (Rank() > 1) { if (Rank() > 1) {
o << "reshape("; o << "reshape(";
} }
@ -97,11 +99,12 @@ std::ostream &Constant<Type<TypeCategory::Character, KIND>>::AsFortran(
return o; return o;
} }
std::ostream &ActualArgument::AssumedType::AsFortran(std::ostream &o) const { llvm::raw_ostream &ActualArgument::AssumedType::AsFortran(
llvm::raw_ostream &o) const {
return o << symbol_->name().ToString(); return o << symbol_->name().ToString();
} }
std::ostream &ActualArgument::AsFortran(std::ostream &o) const { llvm::raw_ostream &ActualArgument::AsFortran(llvm::raw_ostream &o) const {
if (keyword_) { if (keyword_) {
o << keyword_->ToString() << '='; o << keyword_->ToString() << '=';
} }
@ -115,11 +118,11 @@ std::ostream &ActualArgument::AsFortran(std::ostream &o) const {
} }
} }
std::ostream &SpecificIntrinsic::AsFortran(std::ostream &o) const { llvm::raw_ostream &SpecificIntrinsic::AsFortran(llvm::raw_ostream &o) const {
return o << name; return o << name;
} }
std::ostream &ProcedureRef::AsFortran(std::ostream &o) const { llvm::raw_ostream &ProcedureRef::AsFortran(llvm::raw_ostream &o) const {
for (const auto &arg : arguments_) { for (const auto &arg : arguments_) {
if (arg && arg->isPassedObject()) { if (arg && arg->isPassedObject()) {
arg->AsFortran(o) << '%'; arg->AsFortran(o) << '%';
@ -306,7 +309,8 @@ static OperatorSpelling SpellOperator(const Relational<T> &x) {
} }
template<typename D, typename R, typename... O> template<typename D, typename R, typename... O>
std::ostream &Operation<D, R, O...>::AsFortran(std::ostream &o) const { llvm::raw_ostream &Operation<D, R, O...>::AsFortran(
llvm::raw_ostream &o) const {
Precedence lhsPrec{ToPrecedence(left())}; Precedence lhsPrec{ToPrecedence(left())};
OperatorSpelling spelling{SpellOperator(derived())}; OperatorSpelling spelling{SpellOperator(derived())};
o << spelling.prefix; o << spelling.prefix;
@ -337,7 +341,7 @@ std::ostream &Operation<D, R, O...>::AsFortran(std::ostream &o) const {
} }
template<typename TO, TypeCategory FROMCAT> template<typename TO, TypeCategory FROMCAT>
std::ostream &Convert<TO, FROMCAT>::AsFortran(std::ostream &o) const { llvm::raw_ostream &Convert<TO, FROMCAT>::AsFortran(llvm::raw_ostream &o) const {
static_assert(TO::category == TypeCategory::Integer || static_assert(TO::category == TypeCategory::Integer ||
TO::category == TypeCategory::Real || TO::category == TypeCategory::Real ||
TO::category == TypeCategory::Character || TO::category == TypeCategory::Character ||
@ -355,21 +359,22 @@ std::ostream &Convert<TO, FROMCAT>::AsFortran(std::ostream &o) const {
return o << ",kind=" << TO::kind << ')'; return o << ",kind=" << TO::kind << ')';
} }
std::ostream &Relational<SomeType>::AsFortran(std::ostream &o) const { llvm::raw_ostream &Relational<SomeType>::AsFortran(llvm::raw_ostream &o) const {
std::visit([&](const auto &rel) { rel.AsFortran(o); }, u); std::visit([&](const auto &rel) { rel.AsFortran(o); }, u);
return o; return o;
} }
template<typename T> template<typename T>
std::ostream &EmitArray(std::ostream &o, const Expr<T> &expr) { llvm::raw_ostream &EmitArray(llvm::raw_ostream &o, const Expr<T> &expr) {
return expr.AsFortran(o); return expr.AsFortran(o);
} }
template<typename T> template<typename T>
std::ostream &EmitArray(std::ostream &, const ArrayConstructorValues<T> &); llvm::raw_ostream &EmitArray(
llvm::raw_ostream &, const ArrayConstructorValues<T> &);
template<typename T> template<typename T>
std::ostream &EmitArray(std::ostream &o, const ImpliedDo<T> &implDo) { llvm::raw_ostream &EmitArray(llvm::raw_ostream &o, const ImpliedDo<T> &implDo) {
o << '('; o << '(';
EmitArray(o, implDo.values()); EmitArray(o, implDo.values());
o << ',' << ImpliedDoIndex::Result::AsFortran() o << ',' << ImpliedDoIndex::Result::AsFortran()
@ -381,8 +386,8 @@ std::ostream &EmitArray(std::ostream &o, const ImpliedDo<T> &implDo) {
} }
template<typename T> template<typename T>
std::ostream &EmitArray( llvm::raw_ostream &EmitArray(
std::ostream &o, const ArrayConstructorValues<T> &values) { llvm::raw_ostream &o, const ArrayConstructorValues<T> &values) {
const char *sep{""}; const char *sep{""};
for (const auto &value : values) { for (const auto &value : values) {
o << sep; o << sep;
@ -393,21 +398,23 @@ std::ostream &EmitArray(
} }
template<typename T> template<typename T>
std::ostream &ArrayConstructor<T>::AsFortran(std::ostream &o) const { llvm::raw_ostream &ArrayConstructor<T>::AsFortran(llvm::raw_ostream &o) const {
o << '[' << GetType().AsFortran() << "::"; o << '[' << GetType().AsFortran() << "::";
EmitArray(o, *this); EmitArray(o, *this);
return o << ']'; return o << ']';
} }
template<int KIND> template<int KIND>
std::ostream &ArrayConstructor<Type<TypeCategory::Character, KIND>>::AsFortran( llvm::raw_ostream &
std::ostream &o) const { ArrayConstructor<Type<TypeCategory::Character, KIND>>::AsFortran(
llvm::raw_ostream &o) const {
o << '[' << GetType().AsFortran(LEN().AsFortran()) << "::"; o << '[' << GetType().AsFortran(LEN().AsFortran()) << "::";
EmitArray(o, *this); EmitArray(o, *this);
return o << ']'; return o << ']';
} }
std::ostream &ArrayConstructor<SomeDerived>::AsFortran(std::ostream &o) const { llvm::raw_ostream &ArrayConstructor<SomeDerived>::AsFortran(
llvm::raw_ostream &o) const {
o << '[' << GetType().AsFortran() << "::"; o << '[' << GetType().AsFortran() << "::";
EmitArray(o, *this); EmitArray(o, *this);
return o << ']'; return o << ']';
@ -415,13 +422,15 @@ std::ostream &ArrayConstructor<SomeDerived>::AsFortran(std::ostream &o) const {
template<typename RESULT> template<typename RESULT>
std::string ExpressionBase<RESULT>::AsFortran() const { std::string ExpressionBase<RESULT>::AsFortran() const {
std::ostringstream ss; std::string buf;
llvm::raw_string_ostream ss{buf};
AsFortran(ss); AsFortran(ss);
return ss.str(); return ss.str();
} }
template<typename RESULT> template<typename RESULT>
std::ostream &ExpressionBase<RESULT>::AsFortran(std::ostream &o) const { llvm::raw_ostream &ExpressionBase<RESULT>::AsFortran(
llvm::raw_ostream &o) const {
std::visit( std::visit(
common::visitors{ common::visitors{
[&](const BOZLiteralConstant &x) { [&](const BOZLiteralConstant &x) {
@ -438,7 +447,7 @@ std::ostream &ExpressionBase<RESULT>::AsFortran(std::ostream &o) const {
return o; return o;
} }
std::ostream &StructureConstructor::AsFortran(std::ostream &o) const { llvm::raw_ostream &StructureConstructor::AsFortran(llvm::raw_ostream &o) const {
o << DerivedTypeSpecAsFortran(result_.derivedTypeSpec()); o << DerivedTypeSpecAsFortran(result_.derivedTypeSpec());
if (values_.empty()) { if (values_.empty()) {
o << '('; o << '(';
@ -496,7 +505,8 @@ std::string SomeDerived::AsFortran() const {
} }
std::string DerivedTypeSpecAsFortran(const semantics::DerivedTypeSpec &spec) { std::string DerivedTypeSpecAsFortran(const semantics::DerivedTypeSpec &spec) {
std::stringstream ss; std::string buf;
llvm::raw_string_ostream ss{buf};
ss << spec.name().ToString(); ss << spec.name().ToString();
char ch{'('}; char ch{'('};
for (const auto &[name, value] : spec.parameters()) { for (const auto &[name, value] : spec.parameters()) {
@ -516,33 +526,35 @@ std::string DerivedTypeSpecAsFortran(const semantics::DerivedTypeSpec &spec) {
return ss.str(); return ss.str();
} }
std::ostream &EmitVar(std::ostream &o, const Symbol &symbol) { llvm::raw_ostream &EmitVar(llvm::raw_ostream &o, const Symbol &symbol) {
return o << symbol.name().ToString(); return o << symbol.name().ToString();
} }
std::ostream &EmitVar(std::ostream &o, const std::string &lit) { llvm::raw_ostream &EmitVar(llvm::raw_ostream &o, const std::string &lit) {
return o << parser::QuoteCharacterLiteral(lit); return o << parser::QuoteCharacterLiteral(lit);
} }
std::ostream &EmitVar(std::ostream &o, const std::u16string &lit) { llvm::raw_ostream &EmitVar(llvm::raw_ostream &o, const std::u16string &lit) {
return o << parser::QuoteCharacterLiteral(lit); return o << parser::QuoteCharacterLiteral(lit);
} }
std::ostream &EmitVar(std::ostream &o, const std::u32string &lit) { llvm::raw_ostream &EmitVar(llvm::raw_ostream &o, const std::u32string &lit) {
return o << parser::QuoteCharacterLiteral(lit); return o << parser::QuoteCharacterLiteral(lit);
} }
template<typename A> std::ostream &EmitVar(std::ostream &o, const A &x) { template<typename A>
llvm::raw_ostream &EmitVar(llvm::raw_ostream &o, const A &x) {
return x.AsFortran(o); return x.AsFortran(o);
} }
template<typename A> template<typename A>
std::ostream &EmitVar(std::ostream &o, common::Reference<A> x) { llvm::raw_ostream &EmitVar(llvm::raw_ostream &o, common::Reference<A> x) {
return EmitVar(o, *x); return EmitVar(o, *x);
} }
template<typename A> template<typename A>
std::ostream &EmitVar(std::ostream &o, const A *p, const char *kw = nullptr) { llvm::raw_ostream &EmitVar(
llvm::raw_ostream &o, const A *p, const char *kw = nullptr) {
if (p) { if (p) {
if (kw) { if (kw) {
o << kw; o << kw;
@ -553,8 +565,8 @@ std::ostream &EmitVar(std::ostream &o, const A *p, const char *kw = nullptr) {
} }
template<typename A> template<typename A>
std::ostream &EmitVar( llvm::raw_ostream &EmitVar(
std::ostream &o, const std::optional<A> &x, const char *kw = nullptr) { llvm::raw_ostream &o, const std::optional<A> &x, const char *kw = nullptr) {
if (x) { if (x) {
if (kw) { if (kw) {
o << kw; o << kw;
@ -565,8 +577,8 @@ std::ostream &EmitVar(
} }
template<typename A, bool COPY> template<typename A, bool COPY>
std::ostream &EmitVar(std::ostream &o, const common::Indirection<A, COPY> &p, llvm::raw_ostream &EmitVar(llvm::raw_ostream &o,
const char *kw = nullptr) { const common::Indirection<A, COPY> &p, const char *kw = nullptr) {
if (kw) { if (kw) {
o << kw; o << kw;
} }
@ -575,35 +587,36 @@ std::ostream &EmitVar(std::ostream &o, const common::Indirection<A, COPY> &p,
} }
template<typename A> template<typename A>
std::ostream &EmitVar(std::ostream &o, const std::shared_ptr<A> &p) { llvm::raw_ostream &EmitVar(llvm::raw_ostream &o, const std::shared_ptr<A> &p) {
CHECK(p); CHECK(p);
return EmitVar(o, *p); return EmitVar(o, *p);
} }
template<typename... A> template<typename... A>
std::ostream &EmitVar(std::ostream &o, const std::variant<A...> &u) { llvm::raw_ostream &EmitVar(llvm::raw_ostream &o, const std::variant<A...> &u) {
std::visit([&](const auto &x) { EmitVar(o, x); }, u); std::visit([&](const auto &x) { EmitVar(o, x); }, u);
return o; return o;
} }
std::ostream &BaseObject::AsFortran(std::ostream &o) const { llvm::raw_ostream &BaseObject::AsFortran(llvm::raw_ostream &o) const {
return EmitVar(o, u); return EmitVar(o, u);
} }
template<int KIND> template<int KIND>
std::ostream &TypeParamInquiry<KIND>::AsFortran(std::ostream &o) const { llvm::raw_ostream &TypeParamInquiry<KIND>::AsFortran(
llvm::raw_ostream &o) const {
if (base_) { if (base_) {
return base_->AsFortran(o) << '%'; return base_->AsFortran(o) << '%';
} }
return EmitVar(o, parameter_); return EmitVar(o, parameter_);
} }
std::ostream &Component::AsFortran(std::ostream &o) const { llvm::raw_ostream &Component::AsFortran(llvm::raw_ostream &o) const {
base_.value().AsFortran(o); base_.value().AsFortran(o);
return EmitVar(o << '%', symbol_); return EmitVar(o << '%', symbol_);
} }
std::ostream &NamedEntity::AsFortran(std::ostream &o) const { llvm::raw_ostream &NamedEntity::AsFortran(llvm::raw_ostream &o) const {
std::visit( std::visit(
common::visitors{ common::visitors{
[&](SymbolRef s) { EmitVar(o, s); }, [&](SymbolRef s) { EmitVar(o, s); },
@ -613,18 +626,18 @@ std::ostream &NamedEntity::AsFortran(std::ostream &o) const {
return o; return o;
} }
std::ostream &Triplet::AsFortran(std::ostream &o) const { llvm::raw_ostream &Triplet::AsFortran(llvm::raw_ostream &o) const {
EmitVar(o, lower_) << ':'; EmitVar(o, lower_) << ':';
EmitVar(o, upper_); EmitVar(o, upper_);
EmitVar(o << ':', stride_.value()); EmitVar(o << ':', stride_.value());
return o; return o;
} }
std::ostream &Subscript::AsFortran(std::ostream &o) const { llvm::raw_ostream &Subscript::AsFortran(llvm::raw_ostream &o) const {
return EmitVar(o, u); return EmitVar(o, u);
} }
std::ostream &ArrayRef::AsFortran(std::ostream &o) const { llvm::raw_ostream &ArrayRef::AsFortran(llvm::raw_ostream &o) const {
base_.AsFortran(o); base_.AsFortran(o);
char separator{'('}; char separator{'('};
for (const Subscript &ss : subscript_) { for (const Subscript &ss : subscript_) {
@ -634,7 +647,7 @@ std::ostream &ArrayRef::AsFortran(std::ostream &o) const {
return o << ')'; return o << ')';
} }
std::ostream &CoarrayRef::AsFortran(std::ostream &o) const { llvm::raw_ostream &CoarrayRef::AsFortran(llvm::raw_ostream &o) const {
bool first{true}; bool first{true};
for (const Symbol &part : base_) { for (const Symbol &part : base_) {
if (first) { if (first) {
@ -668,26 +681,26 @@ std::ostream &CoarrayRef::AsFortran(std::ostream &o) const {
return o << ']'; return o << ']';
} }
std::ostream &DataRef::AsFortran(std::ostream &o) const { llvm::raw_ostream &DataRef::AsFortran(llvm::raw_ostream &o) const {
return EmitVar(o, u); return EmitVar(o, u);
} }
std::ostream &Substring::AsFortran(std::ostream &o) const { llvm::raw_ostream &Substring::AsFortran(llvm::raw_ostream &o) const {
EmitVar(o, parent_) << '('; EmitVar(o, parent_) << '(';
EmitVar(o, lower_) << ':'; EmitVar(o, lower_) << ':';
return EmitVar(o, upper_) << ')'; return EmitVar(o, upper_) << ')';
} }
std::ostream &ComplexPart::AsFortran(std::ostream &o) const { llvm::raw_ostream &ComplexPart::AsFortran(llvm::raw_ostream &o) const {
return complex_.AsFortran(o) << '%' << EnumToString(part_); return complex_.AsFortran(o) << '%' << EnumToString(part_);
} }
std::ostream &ProcedureDesignator::AsFortran(std::ostream &o) const { llvm::raw_ostream &ProcedureDesignator::AsFortran(llvm::raw_ostream &o) const {
return EmitVar(o, u); return EmitVar(o, u);
} }
template<typename T> template<typename T>
std::ostream &Designator<T>::AsFortran(std::ostream &o) const { llvm::raw_ostream &Designator<T>::AsFortran(llvm::raw_ostream &o) const {
std::visit( std::visit(
common::visitors{ common::visitors{
[&](SymbolRef symbol) { EmitVar(o, symbol); }, [&](SymbolRef symbol) { EmitVar(o, symbol); },
@ -697,7 +710,7 @@ std::ostream &Designator<T>::AsFortran(std::ostream &o) const {
return o; return o;
} }
std::ostream &DescriptorInquiry::AsFortran(std::ostream &o) const { llvm::raw_ostream &DescriptorInquiry::AsFortran(llvm::raw_ostream &o) const {
switch (field_) { switch (field_) {
case Field::LowerBound: o << "lbound("; break; case Field::LowerBound: o << "lbound("; break;
case Field::Extent: o << "size("; break; case Field::Extent: o << "size("; break;
@ -716,7 +729,7 @@ std::ostream &DescriptorInquiry::AsFortran(std::ostream &o) const {
} }
} }
std::ostream &Assignment::AsFortran(std::ostream &o) const { llvm::raw_ostream &Assignment::AsFortran(llvm::raw_ostream &o) const {
std::visit( std::visit(
common::visitors{ common::visitors{
[&](const Assignment::Intrinsic &) { [&](const Assignment::Intrinsic &) {

View File

@ -9,7 +9,7 @@
#include "host.h" #include "host.h"
#include "flang/Common/idioms.h" #include "flang/Common/idioms.h"
#include <cerrno> #include "llvm/Support/Errno.h"
#include <cfenv> #include <cfenv>
namespace Fortran::evaluate::host { namespace Fortran::evaluate::host {
@ -20,12 +20,12 @@ void HostFloatingPointEnvironment::SetUpHostFloatingPointEnvironment(
errno = 0; errno = 0;
if (feholdexcept(&originalFenv_) != 0) { if (feholdexcept(&originalFenv_) != 0) {
common::die("Folding with host runtime: feholdexcept() failed: %s", common::die("Folding with host runtime: feholdexcept() failed: %s",
std::strerror(errno)); llvm::sys::StrError(errno).c_str());
return; return;
} }
if (fegetenv(&currentFenv_) != 0) { if (fegetenv(&currentFenv_) != 0) {
common::die("Folding with host runtime: fegetenv() failed: %s", common::die("Folding with host runtime: fegetenv() failed: %s",
std::strerror(errno)); llvm::sys::StrError(errno).c_str());
return; return;
} }
#if __x86_64__ #if __x86_64__
@ -72,7 +72,7 @@ void HostFloatingPointEnvironment::SetUpHostFloatingPointEnvironment(
errno = 0; errno = 0;
if (fesetenv(&currentFenv_) != 0) { if (fesetenv(&currentFenv_) != 0) {
common::die("Folding with host runtime: fesetenv() failed: %s", common::die("Folding with host runtime: fesetenv() failed: %s",
std::strerror(errno)); llvm::sys::StrError(errno).c_str());
return; return;
} }
switch (context.rounding().mode) { switch (context.rounding().mode) {
@ -127,10 +127,11 @@ void HostFloatingPointEnvironment::CheckAndRestoreFloatingPointEnvironment(
} }
errno = 0; errno = 0;
if (fesetenv(&originalFenv_) != 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());
common::die( common::die(
"Folding with host runtime: fesetenv() failed while restoring fenv: %s", "Folding with host runtime: fesetenv() failed while restoring fenv: %s",
std::strerror(errno)); llvm::sys::StrError(errno).c_str());
} }
errno = 0; errno = 0;
} }

View File

@ -16,10 +16,9 @@
#include "flang/Evaluate/shape.h" #include "flang/Evaluate/shape.h"
#include "flang/Evaluate/tools.h" #include "flang/Evaluate/tools.h"
#include "flang/Evaluate/type.h" #include "flang/Evaluate/type.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm> #include <algorithm>
#include <map> #include <map>
#include <ostream>
#include <sstream>
#include <string> #include <string>
#include <utility> #include <utility>
@ -90,7 +89,7 @@ ENUM_CLASS(KindCode, none, defaultIntegerKind,
struct TypePattern { struct TypePattern {
CategorySet categorySet; CategorySet categorySet;
KindCode kindCode{KindCode::none}; KindCode kindCode{KindCode::none};
std::ostream &Dump(std::ostream &) const; llvm::raw_ostream &Dump(llvm::raw_ostream &) const;
}; };
// Abbreviations for argument and result patterns in the intrinsic prototypes: // Abbreviations for argument and result patterns in the intrinsic prototypes:
@ -195,7 +194,7 @@ struct IntrinsicDummyArgument {
TypePattern typePattern; TypePattern typePattern;
Rank rank{Rank::elemental}; Rank rank{Rank::elemental};
Optionality optionality{Optionality::required}; Optionality optionality{Optionality::required};
std::ostream &Dump(std::ostream &) const; llvm::raw_ostream &Dump(llvm::raw_ostream &) const;
}; };
// constexpr abbreviations for popular arguments: // constexpr abbreviations for popular arguments:
@ -234,7 +233,7 @@ struct IntrinsicInterface {
const common::IntrinsicTypeDefaultKinds &, ActualArguments &, const common::IntrinsicTypeDefaultKinds &, ActualArguments &,
FoldingContext &context) const; FoldingContext &context) const;
int CountArguments() const; int CountArguments() const;
std::ostream &Dump(std::ostream &) const; llvm::raw_ostream &Dump(llvm::raw_ostream &) const;
}; };
int IntrinsicInterface::CountArguments() const { int IntrinsicInterface::CountArguments() const {
@ -1517,7 +1516,7 @@ public:
std::optional<SpecificIntrinsicFunctionInterface> IsSpecificIntrinsicFunction( std::optional<SpecificIntrinsicFunctionInterface> IsSpecificIntrinsicFunction(
const std::string &) const; const std::string &) const;
std::ostream &Dump(std::ostream &) const; llvm::raw_ostream &Dump(llvm::raw_ostream &) const;
private: private:
DynamicType GetSpecificType(const TypePattern &) const; DynamicType GetSpecificType(const TypePattern &) const;
@ -2000,7 +1999,7 @@ IntrinsicProcTable::IsSpecificIntrinsicFunction(const std::string &name) const {
return DEREF(impl_).IsSpecificIntrinsicFunction(name); return DEREF(impl_).IsSpecificIntrinsicFunction(name);
} }
std::ostream &TypePattern::Dump(std::ostream &o) const { llvm::raw_ostream &TypePattern::Dump(llvm::raw_ostream &o) const {
if (categorySet == AnyType) { if (categorySet == AnyType) {
o << "any type"; o << "any type";
} else { } else {
@ -2016,7 +2015,7 @@ std::ostream &TypePattern::Dump(std::ostream &o) const {
return o; return o;
} }
std::ostream &IntrinsicDummyArgument::Dump(std::ostream &o) const { llvm::raw_ostream &IntrinsicDummyArgument::Dump(llvm::raw_ostream &o) const {
if (keyword) { if (keyword) {
o << keyword << '='; o << keyword << '=';
} }
@ -2024,7 +2023,7 @@ std::ostream &IntrinsicDummyArgument::Dump(std::ostream &o) const {
<< ' ' << EnumToString(rank) << ' ' << EnumToString(optionality); << ' ' << EnumToString(rank) << ' ' << EnumToString(optionality);
} }
std::ostream &IntrinsicInterface::Dump(std::ostream &o) const { llvm::raw_ostream &IntrinsicInterface::Dump(llvm::raw_ostream &o) const {
o << name; o << name;
char sep{'('}; char sep{'('};
for (const auto &d : dummy) { for (const auto &d : dummy) {
@ -2040,7 +2039,8 @@ std::ostream &IntrinsicInterface::Dump(std::ostream &o) const {
return result.Dump(o << " -> ") << ' ' << EnumToString(rank); return result.Dump(o << " -> ") << ' ' << EnumToString(rank);
} }
std::ostream &IntrinsicProcTable::Implementation::Dump(std::ostream &o) const { llvm::raw_ostream &IntrinsicProcTable::Implementation::Dump(
llvm::raw_ostream &o) const {
o << "generic intrinsic functions:\n"; o << "generic intrinsic functions:\n";
for (const auto &iter : genericFuncs_) { for (const auto &iter : genericFuncs_) {
iter.second->Dump(o << iter.first << ": ") << '\n'; iter.second->Dump(o << iter.first << ": ") << '\n';
@ -2060,7 +2060,7 @@ std::ostream &IntrinsicProcTable::Implementation::Dump(std::ostream &o) const {
return o; return o;
} }
std::ostream &IntrinsicProcTable::Dump(std::ostream &o) const { llvm::raw_ostream &IntrinsicProcTable::Dump(llvm::raw_ostream &o) const {
return impl_->Dump(o); return impl_->Dump(o);
} }
} }

View File

@ -11,6 +11,7 @@
#include "flang/Common/idioms.h" #include "flang/Common/idioms.h"
#include "flang/Decimal/decimal.h" #include "flang/Decimal/decimal.h"
#include "flang/Parser/characters.h" #include "flang/Parser/characters.h"
#include "llvm/Support/raw_ostream.h"
#include <limits> #include <limits>
namespace Fortran::evaluate::value { namespace Fortran::evaluate::value {
@ -478,8 +479,8 @@ template<typename W, int P> std::string Real<W, P>::DumpHexadecimal() const {
} }
template<typename W, int P> template<typename W, int P>
std::ostream &Real<W, P>::AsFortran( llvm::raw_ostream &Real<W, P>::AsFortran(
std::ostream &o, int kind, bool minimal) const { llvm::raw_ostream &o, int kind, bool minimal) const {
if (IsNotANumber()) { if (IsNotANumber()) {
o << "(0._" << kind << "/0.)"; o << "(0._" << kind << "/0.)";
} else if (IsInfinite()) { } else if (IsInfinite()) {

View File

@ -13,7 +13,7 @@ namespace Fortran::evaluate {
bool StaticDataObject::bigEndian{false}; bool StaticDataObject::bigEndian{false};
std::ostream &StaticDataObject::AsFortran(std::ostream &o) const { llvm::raw_ostream &StaticDataObject::AsFortran(llvm::raw_ostream &o) const {
if (auto string{AsString()}) { if (auto string{AsString()}) {
o << parser::QuoteCharacterLiteral(*string); o << parser::QuoteCharacterLiteral(*string);
} else if (auto string{AsU16String()}) { } else if (auto string{AsU16String()}) {

View File

@ -18,7 +18,6 @@
#include "flang/Semantics/type.h" #include "flang/Semantics/type.h"
#include <algorithm> #include <algorithm>
#include <optional> #include <optional>
#include <sstream>
#include <string> #include <string>
// IsDescriptor() predicate // IsDescriptor() predicate

View File

@ -14,7 +14,6 @@
#include "flang/Parser/characters.h" #include "flang/Parser/characters.h"
#include "flang/Parser/message.h" #include "flang/Parser/message.h"
#include "flang/Semantics/symbol.h" #include "flang/Semantics/symbol.h"
#include <ostream>
#include <type_traits> #include <type_traits>
using namespace Fortran::parser::literals; using namespace Fortran::parser::literals;

View File

@ -34,6 +34,7 @@ add_library(FortranParser
target_link_libraries(FortranParser target_link_libraries(FortranParser
FortranCommon FortranCommon
LLVMSupport
) )
install (TARGETS FortranParser install (TARGETS FortranParser

View File

@ -7,11 +7,11 @@
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#include "flang/Parser/char-block.h" #include "flang/Parser/char-block.h"
#include <ostream> #include "llvm/Support/raw_ostream.h"
namespace Fortran::parser { namespace Fortran::parser {
std::ostream &operator<<(std::ostream &os, const CharBlock &x) { llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const CharBlock &x) {
return os << x.ToString(); return os << x.ToString();
} }

View File

@ -8,7 +8,6 @@
#include "debug-parser.h" #include "debug-parser.h"
#include "flang/Parser/user-state.h" #include "flang/Parser/user-state.h"
#include <ostream>
#include <string> #include <string>
namespace Fortran::parser { namespace Fortran::parser {

View File

@ -9,8 +9,8 @@
#include "flang/Parser/instrumented-parser.h" #include "flang/Parser/instrumented-parser.h"
#include "flang/Parser/message.h" #include "flang/Parser/message.h"
#include "flang/Parser/provenance.h" #include "flang/Parser/provenance.h"
#include "llvm/Support/raw_ostream.h"
#include <map> #include <map>
#include <ostream>
namespace Fortran::parser { namespace Fortran::parser {
@ -63,7 +63,7 @@ void ParsingLog::Note(const char *at, const MessageFixedText &tag, bool pass,
} }
} }
void ParsingLog::Dump(std::ostream &o, const CookedSource &cooked) const { void ParsingLog::Dump(llvm::raw_ostream &o, const CookedSource &cooked) const {
for (const auto &posLog : perPos_) { for (const auto &posLog : perPos_) {
const char *at{reinterpret_cast<const char *>(posLog.first)}; const char *at{reinterpret_cast<const char *>(posLog.first)};
for (const auto &tagLog : posLog.second.perTag) { for (const auto &tagLog : posLog.second.perTag) {

View File

@ -9,6 +9,7 @@
#include "flang/Parser/message.h" #include "flang/Parser/message.h"
#include "flang/Common/idioms.h" #include "flang/Common/idioms.h"
#include "flang/Parser/char-set.h" #include "flang/Parser/char-set.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm> #include <algorithm>
#include <cstdarg> #include <cstdarg>
#include <cstddef> #include <cstddef>
@ -19,7 +20,7 @@
namespace Fortran::parser { namespace Fortran::parser {
std::ostream &operator<<(std::ostream &o, const MessageFixedText &t) { llvm::raw_ostream &operator<<(llvm::raw_ostream &o, const MessageFixedText &t) {
std::size_t n{t.text().size()}; std::size_t n{t.text().size()};
for (std::size_t j{0}; j < n; ++j) { for (std::size_t j{0}; j < n; ++j) {
o << t.text()[j]; o << t.text()[j];
@ -187,8 +188,8 @@ std::optional<ProvenanceRange> Message::GetProvenanceRange(
location_); location_);
} }
void Message::Emit( void Message::Emit(llvm::raw_ostream &o, const CookedSource &cooked,
std::ostream &o, const CookedSource &cooked, bool echoSourceLine) const { bool echoSourceLine) const {
std::optional<ProvenanceRange> provenanceRange{GetProvenanceRange(cooked)}; std::optional<ProvenanceRange> provenanceRange{GetProvenanceRange(cooked)};
std::string text; std::string text;
if (IsFatal()) { if (IsFatal()) {
@ -306,8 +307,8 @@ void Messages::ResolveProvenances(const CookedSource &cooked) {
} }
} }
void Messages::Emit( void Messages::Emit(llvm::raw_ostream &o, const CookedSource &cooked,
std::ostream &o, const CookedSource &cooked, bool echoSourceLines) const { bool echoSourceLines) const {
std::vector<const Message *> sorted; std::vector<const Message *> sorted;
for (const auto &msg : messages_) { for (const auto &msg : messages_) {
sorted.push_back(&msg); sorted.push_back(&msg);

View File

@ -11,6 +11,7 @@
#include "flang/Common/indirection.h" #include "flang/Common/indirection.h"
#include "flang/Parser/tools.h" #include "flang/Parser/tools.h"
#include "flang/Parser/user-state.h" #include "flang/Parser/user-state.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm> #include <algorithm>
// So "delete Expr;" calls an external destructor for its typedExpr. // So "delete Expr;" calls an external destructor for its typedExpr.
@ -252,7 +253,7 @@ CharBlock Variable::GetSource() const {
u); u);
} }
std::ostream &operator<<(std::ostream &os, const Name &x) { llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Name &x) {
return os << x.ToString(); return os << x.ToString();
} }

View File

@ -13,7 +13,7 @@
#include "flang/Parser/message.h" #include "flang/Parser/message.h"
#include "flang/Parser/provenance.h" #include "flang/Parser/provenance.h"
#include "flang/Parser/source.h" #include "flang/Parser/source.h"
#include <sstream> #include "llvm/Support/raw_ostream.h"
namespace Fortran::parser { namespace Fortran::parser {
@ -29,12 +29,13 @@ const SourceFile *Parsing::Prescan(const std::string &path, Options options) {
} }
} }
std::stringstream fileError; std::string buf;
llvm::raw_string_ostream fileError{buf};
const SourceFile *sourceFile; const SourceFile *sourceFile;
if (path == "-") { if (path == "-") {
sourceFile = allSources.ReadStandardInput(&fileError); sourceFile = allSources.ReadStandardInput(fileError);
} else { } else {
sourceFile = allSources.Open(path, &fileError); sourceFile = allSources.Open(path, fileError);
} }
if (!fileError.str().empty()) { if (!fileError.str().empty()) {
ProvenanceRange range{allSources.AddCompilerInsertion(path)}; ProvenanceRange range{allSources.AddCompilerInsertion(path)};
@ -85,7 +86,7 @@ const SourceFile *Parsing::Prescan(const std::string &path, Options options) {
return sourceFile; return sourceFile;
} }
void Parsing::DumpCookedChars(std::ostream &out) const { void Parsing::DumpCookedChars(llvm::raw_ostream &out) const {
UserState userState{cooked_, common::LanguageFeatureControl{}}; UserState userState{cooked_, common::LanguageFeatureControl{}};
ParseState parseState{cooked_}; ParseState parseState{cooked_};
parseState.set_inFixedForm(options_.isFixedForm).set_userState(&userState); parseState.set_inFixedForm(options_.isFixedForm).set_userState(&userState);
@ -94,13 +95,15 @@ void Parsing::DumpCookedChars(std::ostream &out) const {
} }
} }
void Parsing::DumpProvenance(std::ostream &out) const { cooked_.Dump(out); } void Parsing::DumpProvenance(llvm::raw_ostream &out) const {
cooked_.Dump(out);
}
void Parsing::DumpParsingLog(std::ostream &out) const { void Parsing::DumpParsingLog(llvm::raw_ostream &out) const {
log_.Dump(out, cooked_); log_.Dump(out, cooked_);
} }
void Parsing::Parse(std::ostream *out) { void Parsing::Parse(llvm::raw_ostream &out) {
UserState userState{cooked_, options_.features}; UserState userState{cooked_, options_.features};
userState.set_debugOutput(out) userState.set_debugOutput(out)
.set_instrumentedParse(options_.instrumentedParse) .set_instrumentedParse(options_.instrumentedParse)
@ -117,14 +120,15 @@ void Parsing::Parse(std::ostream *out) {
void Parsing::ClearLog() { log_.clear(); } void Parsing::ClearLog() { log_.clear(); }
bool Parsing::ForTesting(std::string path, std::ostream &err) { bool Parsing::ForTesting(std::string path, llvm::raw_ostream &err) {
llvm::raw_null_ostream NullStream;
Prescan(path, Options{}); Prescan(path, Options{});
if (messages_.AnyFatalError()) { if (messages_.AnyFatalError()) {
messages_.Emit(err, cooked_); messages_.Emit(err, cooked_);
err << "could not scan " << path << '\n'; err << "could not scan " << path << '\n';
return false; return false;
} }
Parse(); Parse(NullStream);
messages_.Emit(err, cooked_); messages_.Emit(err, cooked_);
if (!consumedWholeFile_) { if (!consumedWholeFile_) {
EmitMessage(err, finalRestingPlace_, "parser FAIL; final position"); EmitMessage(err, finalRestingPlace_, "parser FAIL; final position");

View File

@ -11,6 +11,7 @@
#include "flang/Common/idioms.h" #include "flang/Common/idioms.h"
#include "flang/Parser/characters.h" #include "flang/Parser/characters.h"
#include "flang/Parser/message.h" #include "flang/Parser/message.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm> #include <algorithm>
#include <cinttypes> #include <cinttypes>
#include <cstddef> #include <cstddef>
@ -19,7 +20,6 @@
#include <memory> #include <memory>
#include <optional> #include <optional>
#include <set> #include <set>
#include <sstream>
#include <utility> #include <utility>
namespace Fortran::parser { namespace Fortran::parser {
@ -257,7 +257,8 @@ std::optional<TokenSequence> Preprocessor::MacroReplacement(
repl = "\""s + repl = "\""s +
allSources_.GetPath(prescanner.GetCurrentProvenance()) + '"'; allSources_.GetPath(prescanner.GetCurrentProvenance()) + '"';
} else if (name == "__LINE__") { } else if (name == "__LINE__") {
std::stringstream ss; std::string buf;
llvm::raw_string_ostream ss{buf};
ss << allSources_.GetLineNumber(prescanner.GetCurrentProvenance()); ss << allSources_.GetLineNumber(prescanner.GetCurrentProvenance());
repl = ss.str(); repl = ss.str();
} }
@ -574,8 +575,9 @@ void Preprocessor::Directive(const TokenSequence &dir, Prescanner *prescanner) {
"#include: empty include file name"_err_en_US); "#include: empty include file name"_err_en_US);
return; return;
} }
std::stringstream error; std::string buf;
const SourceFile *included{allSources_.Open(include, &error)}; llvm::raw_string_ostream error{buf};
const SourceFile *included{allSources_.Open(include, error)};
if (!included) { if (!included) {
prescanner->Say(dir.GetTokenProvenanceRange(dirOffset), prescanner->Say(dir.GetTokenProvenanceRange(dirOffset),
"#include: %s"_err_en_US, error.str()); "#include: %s"_err_en_US, error.str());

View File

@ -13,9 +13,9 @@
#include "flang/Parser/characters.h" #include "flang/Parser/characters.h"
#include "flang/Parser/message.h" #include "flang/Parser/message.h"
#include "flang/Parser/source.h" #include "flang/Parser/source.h"
#include "llvm/Support/raw_ostream.h"
#include <cstddef> #include <cstddef>
#include <cstring> #include <cstring>
#include <sstream>
#include <utility> #include <utility>
#include <vector> #include <vector>
@ -736,14 +736,15 @@ void Prescanner::FortranInclude(const char *firstQuote) {
Say(GetProvenanceRange(garbage, p), Say(GetProvenanceRange(garbage, p),
"excess characters after path name"_en_US); "excess characters after path name"_en_US);
} }
std::stringstream error; std::string buf;
llvm::raw_string_ostream error{buf};
Provenance provenance{GetProvenance(nextLine_)}; Provenance provenance{GetProvenance(nextLine_)};
AllSources &allSources{cooked_.allSources()}; AllSources &allSources{cooked_.allSources()};
const SourceFile *currentFile{allSources.GetSourceFile(provenance)}; const SourceFile *currentFile{allSources.GetSourceFile(provenance)};
if (currentFile) { if (currentFile) {
allSources.PushSearchPathDirectory(DirectoryName(currentFile->path())); allSources.PushSearchPathDirectory(DirectoryName(currentFile->path()));
} }
const SourceFile *included{allSources.Open(path, &error)}; const SourceFile *included{allSources.Open(path, error)};
if (currentFile) { if (currentFile) {
allSources.PopSearchPathDirectory(); allSources.PopSearchPathDirectory();
} }

View File

@ -8,6 +8,7 @@
#include "flang/Parser/provenance.h" #include "flang/Parser/provenance.h"
#include "flang/Common/idioms.h" #include "flang/Common/idioms.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm> #include <algorithm>
#include <utility> #include <utility>
@ -166,7 +167,7 @@ std::string AllSources::PopSearchPathDirectory() {
return directory; return directory;
} }
const SourceFile *AllSources::Open(std::string path, std::stringstream *error) { const SourceFile *AllSources::Open(std::string path, llvm::raw_ostream &error) {
std::unique_ptr<SourceFile> source{std::make_unique<SourceFile>(encoding_)}; std::unique_ptr<SourceFile> source{std::make_unique<SourceFile>(encoding_)};
if (source->Open(LocateSourceFile(path, searchPath_), error)) { if (source->Open(LocateSourceFile(path, searchPath_), error)) {
return ownedSourceFiles_.emplace_back(std::move(source)).get(); return ownedSourceFiles_.emplace_back(std::move(source)).get();
@ -175,7 +176,7 @@ const SourceFile *AllSources::Open(std::string path, std::stringstream *error) {
} }
} }
const SourceFile *AllSources::ReadStandardInput(std::stringstream *error) { const SourceFile *AllSources::ReadStandardInput(llvm::raw_ostream &error) {
std::unique_ptr<SourceFile> source{std::make_unique<SourceFile>(encoding_)}; std::unique_ptr<SourceFile> source{std::make_unique<SourceFile>(encoding_)};
if (source->ReadStandardInput(error)) { if (source->ReadStandardInput(error)) {
return ownedSourceFiles_.emplace_back(std::move(source)).get(); return ownedSourceFiles_.emplace_back(std::move(source)).get();
@ -209,7 +210,7 @@ ProvenanceRange AllSources::AddCompilerInsertion(std::string text) {
return covers; return covers;
} }
void AllSources::EmitMessage(std::ostream &o, void AllSources::EmitMessage(llvm::raw_ostream &o,
const std::optional<ProvenanceRange> &range, const std::string &message, const std::optional<ProvenanceRange> &range, const std::string &message,
bool echoSourceLine) const { bool echoSourceLine) const {
if (!range) { if (!range) {
@ -470,12 +471,13 @@ void CookedSource::CompileProvenanceRangeToOffsetMappings() {
} }
} }
static void DumpRange(std::ostream &o, const ProvenanceRange &r) { static void DumpRange(llvm::raw_ostream &o, const ProvenanceRange &r) {
o << "[" << r.start().offset() << ".." << r.Last().offset() << "] (" o << "[" << r.start().offset() << ".." << r.Last().offset() << "] ("
<< r.size() << " bytes)"; << r.size() << " bytes)";
} }
std::ostream &ProvenanceRangeToOffsetMappings::Dump(std::ostream &o) const { llvm::raw_ostream &ProvenanceRangeToOffsetMappings::Dump(
llvm::raw_ostream &o) const {
for (const auto &m : map_) { for (const auto &m : map_) {
o << "provenances "; o << "provenances ";
DumpRange(o, m.first); DumpRange(o, m.first);
@ -485,7 +487,8 @@ std::ostream &ProvenanceRangeToOffsetMappings::Dump(std::ostream &o) const {
return o; return o;
} }
std::ostream &OffsetToProvenanceMappings::Dump(std::ostream &o) const { llvm::raw_ostream &OffsetToProvenanceMappings::Dump(
llvm::raw_ostream &o) const {
for (const ContiguousProvenanceMapping &m : provenanceMap_) { for (const ContiguousProvenanceMapping &m : provenanceMap_) {
std::size_t n{m.range.size()}; std::size_t n{m.range.size()};
o << "offsets [" << m.start << ".." << (m.start + n - 1) o << "offsets [" << m.start << ".." << (m.start + n - 1)
@ -496,7 +499,7 @@ std::ostream &OffsetToProvenanceMappings::Dump(std::ostream &o) const {
return o; return o;
} }
std::ostream &AllSources::Dump(std::ostream &o) const { llvm::raw_ostream &AllSources::Dump(llvm::raw_ostream &o) const {
o << "AllSources range_ "; o << "AllSources range_ ";
DumpRange(o, range_); DumpRange(o, range_);
o << '\n'; o << '\n';
@ -517,7 +520,8 @@ std::ostream &AllSources::Dump(std::ostream &o) const {
o << "compiler '" << ins.text << '\''; o << "compiler '" << ins.text << '\'';
if (ins.text.length() == 1) { if (ins.text.length() == 1) {
int ch = ins.text[0]; int ch = ins.text[0];
o << " (0x" << std::hex << (ch & 0xff) << std::dec << ")"; o << "(0x";
o.write_hex(ch & 0xff) << ")";
} }
}, },
}, },
@ -531,7 +535,7 @@ std::ostream &AllSources::Dump(std::ostream &o) const {
return o; return o;
} }
std::ostream &CookedSource::Dump(std::ostream &o) const { llvm::raw_ostream &CookedSource::Dump(llvm::raw_ostream &o) const {
o << "CookedSource:\n"; o << "CookedSource:\n";
allSources_.Dump(o); allSources_.Dump(o);
o << "CookedSource::provenanceMap_:\n"; o << "CookedSource::provenanceMap_:\n";

View File

@ -9,8 +9,9 @@
#include "flang/Parser/source.h" #include "flang/Parser/source.h"
#include "flang/Common/idioms.h" #include "flang/Common/idioms.h"
#include "flang/Parser/char-buffer.h" #include "flang/Parser/char-buffer.h"
#include "llvm/Support/Errno.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm> #include <algorithm>
#include <cerrno>
#include <cstddef> #include <cstddef>
#include <cstring> #include <cstring>
#include <fcntl.h> #include <fcntl.h>
@ -111,36 +112,38 @@ static std::size_t RemoveCarriageReturns(char *buffer, std::size_t bytes) {
return wrote; return wrote;
} }
bool SourceFile::Open(std::string path, std::stringstream *error) { bool SourceFile::Open(std::string path, llvm::raw_ostream &error) {
Close(); Close();
path_ = path; path_ = path;
std::string errorPath{"'"s + path + "'"}; std::string errorPath{"'"s + path + "'"};
errno = 0; errno = 0;
fileDescriptor_ = open(path.c_str(), O_RDONLY); fileDescriptor_ = open(path.c_str(), O_RDONLY);
if (fileDescriptor_ < 0) { if (fileDescriptor_ < 0) {
*error << "Could not open " << errorPath << ": " << std::strerror(errno); error << "Could not open " << errorPath << ": "
<< llvm::sys::StrError(errno);
return false; return false;
} }
++openFileDescriptors; ++openFileDescriptors;
return ReadFile(errorPath, error); return ReadFile(errorPath, error);
} }
bool SourceFile::ReadStandardInput(std::stringstream *error) { bool SourceFile::ReadStandardInput(llvm::raw_ostream &error) {
Close(); Close();
path_ = "standard input"; path_ = "standard input";
fileDescriptor_ = 0; fileDescriptor_ = 0;
return ReadFile(path_, error); return ReadFile(path_, error);
} }
bool SourceFile::ReadFile(std::string errorPath, std::stringstream *error) { bool SourceFile::ReadFile(std::string errorPath, llvm::raw_ostream &error) {
struct stat statbuf; struct stat statbuf;
if (fstat(fileDescriptor_, &statbuf) != 0) { if (fstat(fileDescriptor_, &statbuf) != 0) {
*error << "fstat failed on " << errorPath << ": " << std::strerror(errno); error << "fstat failed on " << errorPath << ": "
<< llvm::sys::StrError(errno);
Close(); Close();
return false; return false;
} }
if (S_ISDIR(statbuf.st_mode)) { if (S_ISDIR(statbuf.st_mode)) {
*error << errorPath << " is a directory"; error << errorPath << " is a directory";
Close(); Close();
return false; return false;
} }
@ -203,7 +206,8 @@ bool SourceFile::ReadFile(std::string errorPath, std::stringstream *error) {
char *to{buffer.FreeSpace(count)}; char *to{buffer.FreeSpace(count)};
ssize_t got{read(fileDescriptor_, to, count)}; ssize_t got{read(fileDescriptor_, to, count)};
if (got < 0) { if (got < 0) {
*error << "could not read " << errorPath << ": " << std::strerror(errno); error << "could not read " << errorPath << ": "
<< llvm::sys::StrError(errno);
Close(); Close();
return false; return false;
} }

View File

@ -8,6 +8,7 @@
#include "token-sequence.h" #include "token-sequence.h"
#include "flang/Parser/characters.h" #include "flang/Parser/characters.h"
#include "llvm/Support/raw_ostream.h"
namespace Fortran::parser { namespace Fortran::parser {
@ -123,7 +124,7 @@ void TokenSequence::Put(const std::string &s, Provenance provenance) {
Put(s.data(), s.size(), provenance); Put(s.data(), s.size(), provenance);
} }
void TokenSequence::Put(const std::stringstream &ss, Provenance provenance) { void TokenSequence::Put(llvm::raw_string_ostream &ss, Provenance provenance) {
Put(ss.str(), provenance); Put(ss.str(), provenance);
} }
@ -248,7 +249,7 @@ void TokenSequence::Emit(CookedSource &cooked) const {
cooked.PutProvenanceMappings(provenances_); cooked.PutProvenanceMappings(provenances_);
} }
void TokenSequence::Dump(std::ostream &o) const { void TokenSequence::Dump(llvm::raw_ostream &o) const {
o << "TokenSequence has " << char_.size() << " chars; nextStart_ " o << "TokenSequence has " << char_.size() << " chars; nextStart_ "
<< nextStart_ << '\n'; << nextStart_ << '\n';
for (std::size_t j{0}; j < start_.size(); ++j) { for (std::size_t j{0}; j < start_.size(); ++j) {

View File

@ -17,11 +17,14 @@
#include "flang/Parser/provenance.h" #include "flang/Parser/provenance.h"
#include <cstddef> #include <cstddef>
#include <cstring> #include <cstring>
#include <ostream>
#include <string> #include <string>
#include <utility> #include <utility>
#include <vector> #include <vector>
namespace llvm {
class raw_ostream;
}
namespace Fortran::parser { namespace Fortran::parser {
// Buffers a contiguous sequence of characters that has been partitioned into // Buffers a contiguous sequence of characters that has been partitioned into
@ -91,7 +94,7 @@ public:
void Put(const char *, std::size_t, Provenance); void Put(const char *, std::size_t, Provenance);
void Put(const CharBlock &, Provenance); void Put(const CharBlock &, Provenance);
void Put(const std::string &, Provenance); void Put(const std::string &, Provenance);
void Put(const std::stringstream &, Provenance); void Put(llvm::raw_string_ostream &, Provenance);
Provenance GetTokenProvenance( Provenance GetTokenProvenance(
std::size_t token, std::size_t offset = 0) const; std::size_t token, std::size_t offset = 0) const;
@ -109,7 +112,7 @@ public:
TokenSequence &RemoveRedundantBlanks(std::size_t firstChar = 0); TokenSequence &RemoveRedundantBlanks(std::size_t firstChar = 0);
TokenSequence &ClipComment(bool skipFirst = false); TokenSequence &ClipComment(bool skipFirst = false);
void Emit(CookedSource &) const; void Emit(CookedSource &) const;
void Dump(std::ostream &) const; void Dump(llvm::raw_ostream &) const;
private: private:
std::size_t TokenBytes(std::size_t token) const { std::size_t TokenBytes(std::size_t token) const {

View File

@ -16,6 +16,7 @@
#include "flang/Parser/characters.h" #include "flang/Parser/characters.h"
#include "flang/Parser/parse-tree-visitor.h" #include "flang/Parser/parse-tree-visitor.h"
#include "flang/Parser/parse-tree.h" #include "flang/Parser/parse-tree.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm> #include <algorithm>
#include <cinttypes> #include <cinttypes>
#include <cstddef> #include <cstddef>
@ -25,9 +26,9 @@ namespace Fortran::parser {
class UnparseVisitor { class UnparseVisitor {
public: public:
UnparseVisitor(std::ostream &out, int indentationAmount, Encoding encoding, UnparseVisitor(llvm::raw_ostream &out, int indentationAmount,
bool capitalize, bool backslashEscapes, preStatementType *preStatement, Encoding encoding, bool capitalize, bool backslashEscapes,
AnalyzedObjectsAsFortran *asFortran) preStatementType *preStatement, AnalyzedObjectsAsFortran *asFortran)
: out_{out}, indentationAmount_{indentationAmount}, encoding_{encoding}, : out_{out}, indentationAmount_{indentationAmount}, encoding_{encoding},
capitalizeKeywords_{capitalize}, backslashEscapes_{backslashEscapes}, capitalizeKeywords_{capitalize}, backslashEscapes_{backslashEscapes},
preStatement_{preStatement}, asFortran_{asFortran} {} preStatement_{preStatement}, asFortran_{asFortran} {}
@ -2511,7 +2512,7 @@ private:
structureComponents_.clear(); structureComponents_.clear();
} }
std::ostream &out_; llvm::raw_ostream &out_;
int indent_{0}; int indent_{0};
const int indentationAmount_{1}; const int indentationAmount_{1};
int column_{1}; int column_{1};
@ -2593,7 +2594,7 @@ void UnparseVisitor::Word(const char *str) {
void UnparseVisitor::Word(const std::string &str) { Word(str.c_str()); } void UnparseVisitor::Word(const std::string &str) { Word(str.c_str()); }
void Unparse(std::ostream &out, const Program &program, Encoding encoding, void Unparse(llvm::raw_ostream &out, const Program &program, Encoding encoding,
bool capitalizeKeywords, bool backslashEscapes, bool capitalizeKeywords, bool backslashEscapes,
preStatementType *preStatement, AnalyzedObjectsAsFortran *asFortran) { preStatementType *preStatement, AnalyzedObjectsAsFortran *asFortran) {
UnparseVisitor visitor{out, 1, encoding, capitalizeKeywords, backslashEscapes, UnparseVisitor visitor{out, 1, encoding, capitalizeKeywords, backslashEscapes,

View File

@ -8,7 +8,7 @@
#include "flang/Semantics/attr.h" #include "flang/Semantics/attr.h"
#include "flang/Common/idioms.h" #include "flang/Common/idioms.h"
#include <ostream> #include "llvm/Support/raw_ostream.h"
#include <stddef.h> #include <stddef.h>
namespace Fortran::semantics { namespace Fortran::semantics {
@ -29,11 +29,11 @@ std::string AttrToString(Attr attr) {
} }
} }
std::ostream &operator<<(std::ostream &o, Attr attr) { llvm::raw_ostream &operator<<(llvm::raw_ostream &o, Attr attr) {
return o << AttrToString(attr); return o << AttrToString(attr);
} }
std::ostream &operator<<(std::ostream &o, const Attrs &attrs) { llvm::raw_ostream &operator<<(llvm::raw_ostream &o, const Attrs &attrs) {
std::size_t n{attrs.count()}; std::size_t n{attrs.count()};
std::size_t seen{0}; std::size_t seen{0};
for (std::size_t j{0}; seen < n; ++j) { for (std::size_t j{0}; seen < n; ++j) {

View File

@ -22,11 +22,11 @@
#include "flang/Semantics/semantics.h" #include "flang/Semantics/semantics.h"
#include "flang/Semantics/symbol.h" #include "flang/Semantics/symbol.h"
#include "flang/Semantics/tools.h" #include "flang/Semantics/tools.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm> #include <algorithm>
#include <functional> #include <functional>
#include <optional> #include <optional>
#include <set> #include <set>
#include <sstream>
// Typedef for optional generic expressions (ubiquitous in this file) // Typedef for optional generic expressions (ubiquitous in this file)
using MaybeExpr = using MaybeExpr =
@ -174,7 +174,7 @@ public:
// Find and return a user-defined assignment // Find and return a user-defined assignment
std::optional<ProcedureRef> TryDefinedAssignment(); std::optional<ProcedureRef> TryDefinedAssignment();
std::optional<ProcedureRef> GetDefinedAssignmentProc(); std::optional<ProcedureRef> GetDefinedAssignmentProc();
void Dump(std::ostream &); void Dump(llvm::raw_ostream &);
private: private:
MaybeExpr TryDefinedOp( MaybeExpr TryDefinedOp(
@ -2426,7 +2426,8 @@ MaybeExpr ExpressionAnalyzer::ExprOrVariable(const PARSED &x) {
x.typedExpr.reset(new GenericExprWrapper{std::move(result)}); x.typedExpr.reset(new GenericExprWrapper{std::move(result)});
if (!x.typedExpr->v) { if (!x.typedExpr->v) {
if (!context_.AnyFatalError()) { if (!context_.AnyFatalError()) {
std::stringstream dump; std::string buf;
llvm::raw_string_ostream dump{buf};
parser::DumpTree(dump, x); parser::DumpTree(dump, x);
Say("Internal error: Expression analysis failed on: %s"_err_en_US, Say("Internal error: Expression analysis failed on: %s"_err_en_US,
dump.str()); dump.str());
@ -2848,7 +2849,7 @@ std::optional<ProcedureRef> ArgumentAnalyzer::GetDefinedAssignmentProc() {
} }
} }
void ArgumentAnalyzer::Dump(std::ostream &os) { void ArgumentAnalyzer::Dump(llvm::raw_ostream &os) {
os << "source_: " << source_.ToString() << " fatalErrors_ = " << fatalErrors_ os << "source_: " << source_.ToString() << " fatalErrors_ = " << fatalErrors_
<< '\n'; << '\n';
for (const auto &actual : actuals_) { for (const auto &actual : actuals_) {

View File

@ -20,7 +20,6 @@
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include <algorithm> #include <algorithm>
#include <fstream> #include <fstream>
#include <ostream>
#include <set> #include <set>
#include <string_view> #include <string_view>
#include <vector> #include <vector>
@ -44,22 +43,23 @@ struct ModHeader {
static std::optional<SourceName> GetSubmoduleParent(const parser::Program &); static std::optional<SourceName> GetSubmoduleParent(const parser::Program &);
static SymbolVector CollectSymbols(const Scope &); static SymbolVector CollectSymbols(const Scope &);
static void PutEntity(std::ostream &, const Symbol &); static void PutEntity(llvm::raw_ostream &, const Symbol &);
static void PutObjectEntity(std::ostream &, const Symbol &); static void PutObjectEntity(llvm::raw_ostream &, const Symbol &);
static void PutProcEntity(std::ostream &, const Symbol &); static void PutProcEntity(llvm::raw_ostream &, const Symbol &);
static void PutPassName(std::ostream &, const std::optional<SourceName> &); static void PutPassName(llvm::raw_ostream &, const std::optional<SourceName> &);
static void PutTypeParam(std::ostream &, const Symbol &); static void PutTypeParam(llvm::raw_ostream &, const Symbol &);
static void PutEntity( static void PutEntity(
std::ostream &, const Symbol &, std::function<void()>, Attrs); llvm::raw_ostream &, const Symbol &, std::function<void()>, Attrs);
static void PutInit(std::ostream &, const Symbol &, const MaybeExpr &); static void PutInit(llvm::raw_ostream &, const Symbol &, const MaybeExpr &);
static void PutInit(std::ostream &, const MaybeIntExpr &); static void PutInit(llvm::raw_ostream &, const MaybeIntExpr &);
static void PutBound(std::ostream &, const Bound &); static void PutBound(llvm::raw_ostream &, const Bound &);
static std::ostream &PutAttrs(std::ostream &, Attrs, static llvm::raw_ostream &PutAttrs(llvm::raw_ostream &, Attrs,
const MaybeExpr & = std::nullopt, std::string before = ","s, const MaybeExpr & = std::nullopt, std::string before = ","s,
std::string after = ""s); std::string after = ""s);
static std::ostream &PutAttr(std::ostream &, Attr);
static std::ostream &PutType(std::ostream &, const DeclTypeSpec &); static llvm::raw_ostream &PutAttr(llvm::raw_ostream &, Attr);
static std::ostream &PutLower(std::ostream &, const std::string &); static llvm::raw_ostream &PutType(llvm::raw_ostream &, const DeclTypeSpec &);
static llvm::raw_ostream &PutLower(llvm::raw_ostream &, const std::string &);
static std::error_code WriteFile( static std::error_code WriteFile(
const std::string &, const std::string &, bool = true); const std::string &, const std::string &, bool = true);
static bool FileContentsMatch( static bool FileContentsMatch(
@ -143,7 +143,8 @@ void ModFileWriter::Write(const Symbol &symbol) {
// Return the entire body of the module file // Return the entire body of the module file
// and clear saved uses, decls, and contains. // and clear saved uses, decls, and contains.
std::string ModFileWriter::GetAsString(const Symbol &symbol) { std::string ModFileWriter::GetAsString(const Symbol &symbol) {
std::stringstream all; std::string buf;
llvm::raw_string_ostream all{buf};
auto &details{symbol.get<ModuleDetails>()}; auto &details{symbol.get<ModuleDetails>()};
if (!details.isSubmodule()) { if (!details.isSubmodule()) {
all << "module " << symbol.name(); all << "module " << symbol.name();
@ -157,13 +158,13 @@ std::string ModFileWriter::GetAsString(const Symbol &symbol) {
all << ") " << symbol.name(); all << ") " << symbol.name();
} }
all << '\n' << uses_.str(); all << '\n' << uses_.str();
uses_.str(""s); uses_.str().clear();
all << useExtraAttrs_.str(); all << useExtraAttrs_.str();
useExtraAttrs_.str(""s); useExtraAttrs_.str().clear();
all << decls_.str(); all << decls_.str();
decls_.str(""s); decls_.str().clear();
auto str{contains_.str()}; auto str{contains_.str()};
contains_.str(""s); contains_.str().clear();
if (!str.empty()) { if (!str.empty()) {
all << "contains\n" << str; all << "contains\n" << str;
} }
@ -173,7 +174,9 @@ std::string ModFileWriter::GetAsString(const Symbol &symbol) {
// Put out the visible symbols from scope. // Put out the visible symbols from scope.
void ModFileWriter::PutSymbols(const Scope &scope) { void ModFileWriter::PutSymbols(const Scope &scope) {
std::stringstream typeBindings; // stuff after CONTAINS in derived type std::string buf;
llvm::raw_string_ostream typeBindings{
buf}; // stuff after CONTAINS in derived type
for (const Symbol &symbol : CollectSymbols(scope)) { for (const Symbol &symbol : CollectSymbols(scope)) {
PutSymbol(typeBindings, symbol); PutSymbol(typeBindings, symbol);
} }
@ -186,7 +189,7 @@ void ModFileWriter::PutSymbols(const Scope &scope) {
// Emit a symbol to decls_, except for bindings in a derived type (type-bound // Emit a symbol to decls_, except for bindings in a derived type (type-bound
// procedures, type-bound generics, final procedures) which go to typeBindings. // procedures, type-bound generics, final procedures) which go to typeBindings.
void ModFileWriter::PutSymbol( void ModFileWriter::PutSymbol(
std::stringstream &typeBindings, const Symbol &symbol) { llvm::raw_ostream &typeBindings, const Symbol &symbol) {
std::visit( std::visit(
common::visitors{ common::visitors{
[&](const ModuleDetails &) { /* should be current module */ }, [&](const ModuleDetails &) { /* should be current module */ },
@ -301,13 +304,14 @@ void ModFileWriter::PutSubprogram(const Symbol &symbol) {
Attrs prefixAttrs{subprogramPrefixAttrs & attrs}; Attrs prefixAttrs{subprogramPrefixAttrs & attrs};
// emit any non-prefix attributes in an attribute statement // emit any non-prefix attributes in an attribute statement
attrs &= ~subprogramPrefixAttrs; attrs &= ~subprogramPrefixAttrs;
std::stringstream ss; std::string ssBuf;
llvm::raw_string_ostream ss{ssBuf};
PutAttrs(ss, attrs); PutAttrs(ss, attrs);
if (!ss.str().empty()) { if (!ss.str().empty()) {
decls_ << ss.str().substr(1) << "::" << symbol.name() << '\n'; decls_ << ss.str().substr(1) << "::" << symbol.name() << '\n';
} }
bool isInterface{details.isInterface()}; bool isInterface{details.isInterface()};
std::ostream &os{isInterface ? decls_ : contains_}; llvm::raw_ostream &os{isInterface ? decls_ : contains_};
if (isInterface) { if (isInterface) {
os << "interface\n"; os << "interface\n";
} }
@ -333,7 +337,8 @@ void ModFileWriter::PutSubprogram(const Symbol &symbol) {
// walk symbols, collect ones needed // walk symbols, collect ones needed
ModFileWriter writer{context_}; ModFileWriter writer{context_};
std::stringstream typeBindings; std::string typeBindingsBuf;
llvm::raw_string_ostream typeBindings{typeBindingsBuf};
SubprogramSymbolCollector collector{symbol}; SubprogramSymbolCollector collector{symbol};
collector.Collect(); collector.Collect();
for (const Symbol &need : collector.symbols()) { for (const Symbol &need : collector.symbols()) {
@ -359,7 +364,8 @@ static bool IsIntrinsicOp(const Symbol &symbol) {
} }
} }
static std::ostream &PutGenericName(std::ostream &os, const Symbol &symbol) { static llvm::raw_ostream &PutGenericName(
llvm::raw_ostream &os, const Symbol &symbol) {
if (IsGenericDefinedOp(symbol)) { if (IsGenericDefinedOp(symbol)) {
return os << "operator(" << symbol.name() << ')'; return os << "operator(" << symbol.name() << ')';
} else { } else {
@ -440,7 +446,7 @@ SymbolVector CollectSymbols(const Scope &scope) {
return sorted; return sorted;
} }
void PutEntity(std::ostream &os, const Symbol &symbol) { void PutEntity(llvm::raw_ostream &os, const Symbol &symbol) {
std::visit( std::visit(
common::visitors{ common::visitors{
[&](const ObjectEntityDetails &) { PutObjectEntity(os, symbol); }, [&](const ObjectEntityDetails &) { PutObjectEntity(os, symbol); },
@ -454,7 +460,7 @@ void PutEntity(std::ostream &os, const Symbol &symbol) {
symbol.details()); symbol.details());
} }
void PutShapeSpec(std::ostream &os, const ShapeSpec &x) { void PutShapeSpec(llvm::raw_ostream &os, const ShapeSpec &x) {
if (x.lbound().isAssumed()) { if (x.lbound().isAssumed()) {
CHECK(x.ubound().isAssumed()); CHECK(x.ubound().isAssumed());
os << ".."; os << "..";
@ -468,7 +474,8 @@ void PutShapeSpec(std::ostream &os, const ShapeSpec &x) {
} }
} }
} }
void PutShape(std::ostream &os, const ArraySpec &shape, char open, char close) { void PutShape(
llvm::raw_ostream &os, const ArraySpec &shape, char open, char close) {
if (!shape.empty()) { if (!shape.empty()) {
os << open; os << open;
bool first{true}; bool first{true};
@ -484,7 +491,7 @@ void PutShape(std::ostream &os, const ArraySpec &shape, char open, char close) {
} }
} }
void PutObjectEntity(std::ostream &os, const Symbol &symbol) { void PutObjectEntity(llvm::raw_ostream &os, const Symbol &symbol) {
auto &details{symbol.get<ObjectEntityDetails>()}; auto &details{symbol.get<ObjectEntityDetails>()};
PutEntity(os, symbol, [&]() { PutType(os, DEREF(symbol.GetType())); }, PutEntity(os, symbol, [&]() { PutType(os, DEREF(symbol.GetType())); },
symbol.attrs()); symbol.attrs());
@ -494,7 +501,7 @@ void PutObjectEntity(std::ostream &os, const Symbol &symbol) {
os << '\n'; os << '\n';
} }
void PutProcEntity(std::ostream &os, const Symbol &symbol) { void PutProcEntity(llvm::raw_ostream &os, const Symbol &symbol) {
if (symbol.attrs().test(Attr::INTRINSIC)) { if (symbol.attrs().test(Attr::INTRINSIC)) {
os << "intrinsic::" << symbol.name() << '\n'; os << "intrinsic::" << symbol.name() << '\n';
return; return;
@ -520,13 +527,13 @@ void PutProcEntity(std::ostream &os, const Symbol &symbol) {
os << '\n'; os << '\n';
} }
void PutPassName(std::ostream &os, const std::optional<SourceName> &passName) { void PutPassName(
llvm::raw_ostream &os, const std::optional<SourceName> &passName) {
if (passName) { if (passName) {
os << ",pass(" << *passName << ')'; os << ",pass(" << *passName << ')';
} }
} }
void PutTypeParam(llvm::raw_ostream &os, const Symbol &symbol) {
void PutTypeParam(std::ostream &os, const Symbol &symbol) {
auto &details{symbol.get<TypeParamDetails>()}; auto &details{symbol.get<TypeParamDetails>()};
PutEntity(os, symbol, PutEntity(os, symbol,
[&]() { [&]() {
@ -538,7 +545,8 @@ void PutTypeParam(std::ostream &os, const Symbol &symbol) {
os << '\n'; os << '\n';
} }
void PutInit(std::ostream &os, const Symbol &symbol, const MaybeExpr &init) { void PutInit(
llvm::raw_ostream &os, const Symbol &symbol, const MaybeExpr &init) {
if (init) { if (init) {
if (symbol.attrs().test(Attr::PARAMETER) || if (symbol.attrs().test(Attr::PARAMETER) ||
symbol.owner().IsDerivedType()) { symbol.owner().IsDerivedType()) {
@ -548,13 +556,13 @@ void PutInit(std::ostream &os, const Symbol &symbol, const MaybeExpr &init) {
} }
} }
void PutInit(std::ostream &os, const MaybeIntExpr &init) { void PutInit(llvm::raw_ostream &os, const MaybeIntExpr &init) {
if (init) { if (init) {
init->AsFortran(os << '='); init->AsFortran(os << '=');
} }
} }
void PutBound(std::ostream &os, const Bound &x) { void PutBound(llvm::raw_ostream &os, const Bound &x) {
if (x.isAssumed()) { if (x.isAssumed()) {
os << '*'; os << '*';
} else if (x.isDeferred()) { } else if (x.isDeferred()) {
@ -566,7 +574,7 @@ void PutBound(std::ostream &os, const Bound &x) {
// Write an entity (object or procedure) declaration. // Write an entity (object or procedure) declaration.
// writeType is called to write out the type. // writeType is called to write out the type.
void PutEntity(std::ostream &os, const Symbol &symbol, void PutEntity(llvm::raw_ostream &os, const Symbol &symbol,
std::function<void()> writeType, Attrs attrs) { std::function<void()> writeType, Attrs attrs) {
writeType(); writeType();
MaybeExpr bindName; MaybeExpr bindName;
@ -584,8 +592,8 @@ void PutEntity(std::ostream &os, const Symbol &symbol,
// Put out each attribute to os, surrounded by `before` and `after` and // Put out each attribute to os, surrounded by `before` and `after` and
// mapped to lower case. // mapped to lower case.
std::ostream &PutAttrs(std::ostream &os, Attrs attrs, const MaybeExpr &bindName, llvm::raw_ostream &PutAttrs(llvm::raw_ostream &os, Attrs attrs,
std::string before, std::string after) { const MaybeExpr &bindName, std::string before, std::string after) {
attrs.set(Attr::PUBLIC, false); // no need to write PUBLIC attrs.set(Attr::PUBLIC, false); // no need to write PUBLIC
attrs.set(Attr::EXTERNAL, false); // no need to write EXTERNAL attrs.set(Attr::EXTERNAL, false); // no need to write EXTERNAL
if (bindName) { if (bindName) {
@ -601,15 +609,15 @@ std::ostream &PutAttrs(std::ostream &os, Attrs attrs, const MaybeExpr &bindName,
return os; return os;
} }
std::ostream &PutAttr(std::ostream &os, Attr attr) { llvm::raw_ostream &PutAttr(llvm::raw_ostream &os, Attr attr) {
return PutLower(os, AttrToString(attr)); return PutLower(os, AttrToString(attr));
} }
std::ostream &PutType(std::ostream &os, const DeclTypeSpec &type) { llvm::raw_ostream &PutType(llvm::raw_ostream &os, const DeclTypeSpec &type) {
return PutLower(os, type.AsFortran()); return PutLower(os, type.AsFortran());
} }
std::ostream &PutLower(std::ostream &os, const std::string &str) { llvm::raw_ostream &PutLower(llvm::raw_ostream &os, const std::string &str) {
for (char c : str) { for (char c : str) {
os << parser::ToLowerCaseLetter(c); os << parser::ToLowerCaseLetter(c);
} }
@ -764,8 +772,8 @@ Scope *ModFileReader::Read(const SourceName &name, Scope *ancestor) {
sourceFile->path()); sourceFile->path());
return nullptr; return nullptr;
} }
llvm::raw_null_ostream NullStream;
parsing.Parse(nullptr); parsing.Parse(NullStream);
auto &parseTree{parsing.parseTree()}; auto &parseTree{parsing.parseTree()};
if (!parsing.messages().empty() || !parsing.consumedWholeFile() || if (!parsing.messages().empty() || !parsing.consumedWholeFile() ||
!parseTree) { !parseTree) {

View File

@ -10,7 +10,6 @@
#define FORTRAN_SEMANTICS_MOD_FILE_H_ #define FORTRAN_SEMANTICS_MOD_FILE_H_
#include "flang/Semantics/attr.h" #include "flang/Semantics/attr.h"
#include <sstream>
#include <string> #include <string>
namespace Fortran::parser { namespace Fortran::parser {
@ -19,6 +18,10 @@ class Message;
class MessageFixedText; class MessageFixedText;
} }
namespace llvm {
class raw_ostream;
}
namespace Fortran::semantics { namespace Fortran::semantics {
using SourceName = parser::CharBlock; using SourceName = parser::CharBlock;
@ -33,17 +36,23 @@ public:
private: private:
SemanticsContext &context_; SemanticsContext &context_;
std::stringstream uses_; // Buffer to use with raw_string_ostream
std::stringstream useExtraAttrs_; // attrs added to used entity std::string usesBuf_;
std::stringstream decls_; std::string useExtraAttrsBuf_;
std::stringstream contains_; std::string declsBuf_;
std::string containsBuf_;
llvm::raw_string_ostream uses_{usesBuf_};
llvm::raw_string_ostream useExtraAttrs_{useExtraAttrsBuf_}; // attrs added to used entity
llvm::raw_string_ostream decls_{declsBuf_};
llvm::raw_string_ostream contains_{containsBuf_};
void WriteAll(const Scope &); void WriteAll(const Scope &);
void WriteOne(const Scope &); void WriteOne(const Scope &);
void Write(const Symbol &); void Write(const Symbol &);
std::string GetAsString(const Symbol &); std::string GetAsString(const Symbol &);
void PutSymbols(const Scope &); void PutSymbols(const Scope &);
void PutSymbol(std::stringstream &, const Symbol &); void PutSymbol(llvm::raw_ostream &, const Symbol &);
void PutDerivedType(const Symbol &); void PutDerivedType(const Symbol &);
void PutSubprogram(const Symbol &); void PutSubprogram(const Symbol &);
void PutGeneric(const Symbol &); void PutGeneric(const Symbol &);

View File

@ -19,6 +19,7 @@
#include "flang/Semantics/expression.h" #include "flang/Semantics/expression.h"
#include "flang/Semantics/symbol.h" #include "flang/Semantics/symbol.h"
#include "flang/Semantics/tools.h" #include "flang/Semantics/tools.h"
#include "llvm/Support/raw_ostream.h"
#include <optional> #include <optional>
#include <set> #include <set>
#include <string> #include <string>
@ -232,7 +233,8 @@ void PointerAssignmentChecker::Check(const evaluate::Designator<T> &d) {
if (msg) { if (msg) {
auto restorer{common::ScopedSet(lhs_, last)}; auto restorer{common::ScopedSet(lhs_, last)};
if (auto *m{std::get_if<MessageFixedText>(&*msg)}) { if (auto *m{std::get_if<MessageFixedText>(&*msg)}) {
std::ostringstream ss; std::string buf;
llvm::raw_string_ostream ss{buf};
d.AsFortran(ss); d.AsFortran(ss);
Say(*m, description_, ss.str()); Say(*m, description_, ss.str());
} else { } else {

View File

@ -19,7 +19,6 @@
#include "flang/Semantics/semantics.h" #include "flang/Semantics/semantics.h"
#include "flang/Semantics/tools.h" #include "flang/Semantics/tools.h"
#include <initializer_list> #include <initializer_list>
#include <ostream>
#include <variant> #include <variant>
namespace Fortran::semantics { namespace Fortran::semantics {

View File

@ -33,9 +33,9 @@
#include "flang/Semantics/symbol.h" #include "flang/Semantics/symbol.h"
#include "flang/Semantics/tools.h" #include "flang/Semantics/tools.h"
#include "flang/Semantics/type.h" #include "flang/Semantics/type.h"
#include "llvm/Support/raw_ostream.h"
#include <list> #include <list>
#include <map> #include <map>
#include <ostream>
#include <set> #include <set>
#include <stack> #include <stack>
@ -86,8 +86,10 @@ private:
// the default Fortran mappings nor the mapping defined in parents. // the default Fortran mappings nor the mapping defined in parents.
std::map<char, common::Reference<const DeclTypeSpec>> map_; std::map<char, common::Reference<const DeclTypeSpec>> map_;
friend std::ostream &operator<<(std::ostream &, const ImplicitRules &); friend llvm::raw_ostream &operator<<(
friend void ShowImplicitRule(std::ostream &, const ImplicitRules &, char); llvm::raw_ostream &, const ImplicitRules &);
friend void ShowImplicitRule(
llvm::raw_ostream &, const ImplicitRules &, char);
}; };
// scope -> implicit rules for that scope // scope -> implicit rules for that scope
@ -1463,7 +1465,8 @@ char ImplicitRules::Incr(char ch) {
} }
} }
std::ostream &operator<<(std::ostream &o, const ImplicitRules &implicitRules) { llvm::raw_ostream &operator<<(
llvm::raw_ostream &o, const ImplicitRules &implicitRules) {
o << "ImplicitRules:\n"; o << "ImplicitRules:\n";
for (char ch = 'a'; ch; ch = ImplicitRules::Incr(ch)) { for (char ch = 'a'; ch; ch = ImplicitRules::Incr(ch)) {
ShowImplicitRule(o, implicitRules, ch); ShowImplicitRule(o, implicitRules, ch);
@ -1474,7 +1477,7 @@ std::ostream &operator<<(std::ostream &o, const ImplicitRules &implicitRules) {
return o; return o;
} }
void ShowImplicitRule( void ShowImplicitRule(
std::ostream &o, const ImplicitRules &implicitRules, char ch) { llvm::raw_ostream &o, const ImplicitRules &implicitRules, char ch) {
auto it{implicitRules.map_.find(ch)}; auto it{implicitRules.map_.find(ch)};
if (it != implicitRules.map_.end()) { if (it != implicitRules.map_.end()) {
o << " " << ch << ": " << *it->second << '\n'; o << " " << ch << ": " << *it->second << '\n';

View File

@ -13,6 +13,10 @@
#include <string> #include <string>
#include <vector> #include <vector>
namespace llvm {
class raw_ostream;
}
namespace Fortran::parser { namespace Fortran::parser {
struct Program; struct Program;
} }
@ -24,7 +28,7 @@ class Symbol;
bool ResolveNames(SemanticsContext &, const parser::Program &); bool ResolveNames(SemanticsContext &, const parser::Program &);
void ResolveSpecificationParts(SemanticsContext &, const Symbol &); void ResolveSpecificationParts(SemanticsContext &, const Symbol &);
void DumpSymbols(std::ostream &); void DumpSymbols(llvm::raw_ostream &);
} }

View File

@ -10,9 +10,9 @@
#include "flang/Parser/characters.h" #include "flang/Parser/characters.h"
#include "flang/Semantics/symbol.h" #include "flang/Semantics/symbol.h"
#include "flang/Semantics/type.h" #include "flang/Semantics/type.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm> #include <algorithm>
#include <memory> #include <memory>
#include <sstream>
namespace Fortran::semantics { namespace Fortran::semantics {
@ -32,7 +32,8 @@ bool EquivalenceObject::operator<(const EquivalenceObject &that) const {
} }
std::string EquivalenceObject::AsFortran() const { std::string EquivalenceObject::AsFortran() const {
std::stringstream ss; std::string buf;
llvm::raw_string_ostream ss{buf};
ss << symbol.name().ToString(); ss << symbol.name().ToString();
if (!subscripts.empty()) { if (!subscripts.empty()) {
char sep{'('}; char sep{'('};
@ -283,7 +284,7 @@ void Scope::AddSourceRange(const parser::CharBlock &source) {
} }
} }
std::ostream &operator<<(std::ostream &os, const Scope &scope) { llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Scope &scope) {
os << Scope::EnumToString(scope.kind()) << " scope: "; os << Scope::EnumToString(scope.kind()) << " scope: ";
if (auto *symbol{scope.symbol()}) { if (auto *symbol{scope.symbol()}) {
os << *symbol << ' '; os << *symbol << ' ';

View File

@ -35,12 +35,13 @@
#include "flang/Semantics/expression.h" #include "flang/Semantics/expression.h"
#include "flang/Semantics/scope.h" #include "flang/Semantics/scope.h"
#include "flang/Semantics/symbol.h" #include "flang/Semantics/symbol.h"
#include "llvm/Support/raw_ostream.h"
namespace Fortran::semantics { namespace Fortran::semantics {
using NameToSymbolMap = std::map<const char *, SymbolRef>; using NameToSymbolMap = std::map<const char *, SymbolRef>;
static void DoDumpSymbols(std::ostream &, const Scope &, int indent = 0); static void DoDumpSymbols(llvm::raw_ostream &, const Scope &, int indent = 0);
static void PutIndent(std::ostream &, int indent); static void PutIndent(llvm::raw_ostream &, int indent);
static void GetSymbolNames(const Scope &scope, NameToSymbolMap &symbols) { static void GetSymbolNames(const Scope &scope, NameToSymbolMap &symbols) {
// Finds all symbol names in the scope without collecting duplicates. // Finds all symbol names in the scope without collecting duplicates.
@ -282,15 +283,15 @@ bool Semantics::Perform() {
ModFileWriter{context_}.WriteAll(); ModFileWriter{context_}.WriteAll();
} }
void Semantics::EmitMessages(std::ostream &os) const { void Semantics::EmitMessages(llvm::raw_ostream &os) const {
context_.messages().Emit(os, cooked_); context_.messages().Emit(os, cooked_);
} }
void Semantics::DumpSymbols(std::ostream &os) { void Semantics::DumpSymbols(llvm::raw_ostream &os) {
DoDumpSymbols(os, context_.globalScope()); DoDumpSymbols(os, context_.globalScope());
} }
void Semantics::DumpSymbolsSources(std::ostream &os) const { void Semantics::DumpSymbolsSources(llvm::raw_ostream &os) const {
NameToSymbolMap symbols; NameToSymbolMap symbols;
GetSymbolNames(context_.globalScope(), symbols); GetSymbolNames(context_.globalScope(), symbols);
for (const auto &pair : symbols) { for (const auto &pair : symbols) {
@ -306,7 +307,7 @@ void Semantics::DumpSymbolsSources(std::ostream &os) const {
} }
} }
void DoDumpSymbols(std::ostream &os, const Scope &scope, int indent) { void DoDumpSymbols(llvm::raw_ostream &os, const Scope &scope, int indent) {
PutIndent(os, indent); PutIndent(os, indent);
os << Scope::EnumToString(scope.kind()) << " scope:"; os << Scope::EnumToString(scope.kind()) << " scope:";
if (const auto *symbol{scope.symbol()}) { if (const auto *symbol{scope.symbol()}) {
@ -357,7 +358,7 @@ void DoDumpSymbols(std::ostream &os, const Scope &scope, int indent) {
--indent; --indent;
} }
static void PutIndent(std::ostream &os, int indent) { static void PutIndent(llvm::raw_ostream &os, int indent) {
for (int i = 0; i < indent; ++i) { for (int i = 0; i < indent; ++i) {
os << " "; os << " ";
} }

View File

@ -12,32 +12,32 @@
#include "flang/Semantics/scope.h" #include "flang/Semantics/scope.h"
#include "flang/Semantics/semantics.h" #include "flang/Semantics/semantics.h"
#include "flang/Semantics/tools.h" #include "flang/Semantics/tools.h"
#include <ostream> #include "llvm/Support/raw_ostream.h"
#include <string> #include <string>
namespace Fortran::semantics { namespace Fortran::semantics {
template<typename T> template<typename T>
static void DumpOptional(std::ostream &os, const char *label, const T &x) { static void DumpOptional(llvm::raw_ostream &os, const char *label, const T &x) {
if (x) { if (x) {
os << ' ' << label << ':' << *x; os << ' ' << label << ':' << *x;
} }
} }
template<typename T> template<typename T>
static void DumpExpr(std::ostream &os, const char *label, static void DumpExpr(llvm::raw_ostream &os, const char *label,
const std::optional<evaluate::Expr<T>> &x) { const std::optional<evaluate::Expr<T>> &x) {
if (x) { if (x) {
x->AsFortran(os << ' ' << label << ':'); x->AsFortran(os << ' ' << label << ':');
} }
} }
static void DumpBool(std::ostream &os, const char *label, bool x) { static void DumpBool(llvm::raw_ostream &os, const char *label, bool x) {
if (x) { if (x) {
os << ' ' << label; os << ' ' << label;
} }
} }
static void DumpSymbolVector(std::ostream &os, const SymbolVector &list) { static void DumpSymbolVector(llvm::raw_ostream &os, const SymbolVector &list) {
char sep{' '}; char sep{' '};
for (const Symbol &elem : list) { for (const Symbol &elem : list) {
os << sep << elem.name(); os << sep << elem.name();
@ -45,19 +45,19 @@ static void DumpSymbolVector(std::ostream &os, const SymbolVector &list) {
} }
} }
static void DumpType(std::ostream &os, const Symbol &symbol) { static void DumpType(llvm::raw_ostream &os, const Symbol &symbol) {
if (const auto *type{symbol.GetType()}) { if (const auto *type{symbol.GetType()}) {
os << *type << ' '; os << *type << ' ';
} }
} }
static void DumpType(std::ostream &os, const DeclTypeSpec *type) { static void DumpType(llvm::raw_ostream &os, const DeclTypeSpec *type) {
if (type) { if (type) {
os << ' ' << *type; os << ' ' << *type;
} }
} }
template<typename T> template<typename T>
static void DumpList(std::ostream &os, const char *label, const T &list) { static void DumpList(llvm::raw_ostream &os, const char *label, const T &list) {
if (!list.empty()) { if (!list.empty()) {
os << ' ' << label << ':'; os << ' ' << label << ':';
char sep{' '}; char sep{' '};
@ -81,7 +81,8 @@ void ModuleDetails::set_scope(const Scope *scope) {
scope_ = scope; scope_ = scope;
} }
std::ostream &operator<<(std::ostream &os, const SubprogramDetails &x) { llvm::raw_ostream &operator<<(
llvm::raw_ostream &os, const SubprogramDetails &x) {
DumpBool(os, "isInterface", x.isInterface_); DumpBool(os, "isInterface", x.isInterface_);
DumpExpr(os, "bindName", x.bindName_); DumpExpr(os, "bindName", x.bindName_);
if (x.result_) { if (x.result_) {
@ -334,7 +335,7 @@ bool Symbol::IsFromModFile() const {
ObjectEntityDetails::ObjectEntityDetails(EntityDetails &&d) ObjectEntityDetails::ObjectEntityDetails(EntityDetails &&d)
: EntityDetails(d) {} : EntityDetails(d) {}
std::ostream &operator<<(std::ostream &os, const EntityDetails &x) { llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const EntityDetails &x) {
DumpBool(os, "dummy", x.isDummy()); DumpBool(os, "dummy", x.isDummy());
DumpBool(os, "funcResult", x.isFuncResult()); DumpBool(os, "funcResult", x.isFuncResult());
if (x.type()) { if (x.type()) {
@ -344,7 +345,8 @@ std::ostream &operator<<(std::ostream &os, const EntityDetails &x) {
return os; return os;
} }
std::ostream &operator<<(std::ostream &os, const ObjectEntityDetails &x) { llvm::raw_ostream &operator<<(
llvm::raw_ostream &os, const ObjectEntityDetails &x) {
os << *static_cast<const EntityDetails *>(&x); os << *static_cast<const EntityDetails *>(&x);
DumpList(os, "shape", x.shape()); DumpList(os, "shape", x.shape());
DumpList(os, "coshape", x.coshape()); DumpList(os, "coshape", x.coshape());
@ -352,13 +354,15 @@ std::ostream &operator<<(std::ostream &os, const ObjectEntityDetails &x) {
return os; return os;
} }
std::ostream &operator<<(std::ostream &os, const AssocEntityDetails &x) { llvm::raw_ostream &operator<<(
llvm::raw_ostream &os, const AssocEntityDetails &x) {
os << *static_cast<const EntityDetails *>(&x); os << *static_cast<const EntityDetails *>(&x);
DumpExpr(os, "expr", x.expr()); DumpExpr(os, "expr", x.expr());
return os; return os;
} }
std::ostream &operator<<(std::ostream &os, const ProcEntityDetails &x) { llvm::raw_ostream &operator<<(
llvm::raw_ostream &os, const ProcEntityDetails &x) {
if (auto *symbol{x.interface_.symbol()}) { if (auto *symbol{x.interface_.symbol()}) {
os << ' ' << symbol->name(); os << ' ' << symbol->name();
} else { } else {
@ -376,13 +380,14 @@ std::ostream &operator<<(std::ostream &os, const ProcEntityDetails &x) {
return os; return os;
} }
std::ostream &operator<<(std::ostream &os, const DerivedTypeDetails &x) { llvm::raw_ostream &operator<<(
llvm::raw_ostream &os, const DerivedTypeDetails &x) {
DumpBool(os, "sequence", x.sequence_); DumpBool(os, "sequence", x.sequence_);
DumpList(os, "components", x.componentNames_); DumpList(os, "components", x.componentNames_);
return os; return os;
} }
std::ostream &operator<<(std::ostream &os, const Details &details) { llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Details &details) {
os << DetailsToString(details); os << DetailsToString(details);
std::visit( std::visit(
common::visitors{ common::visitors{
@ -453,11 +458,12 @@ std::ostream &operator<<(std::ostream &os, const Details &details) {
return os; return os;
} }
std::ostream &operator<<(std::ostream &o, Symbol::Flag flag) { llvm::raw_ostream &operator<<(llvm::raw_ostream &o, Symbol::Flag flag) {
return o << Symbol::EnumToString(flag); return o << Symbol::EnumToString(flag);
} }
std::ostream &operator<<(std::ostream &o, const Symbol::Flags &flags) { llvm::raw_ostream &operator<<(
llvm::raw_ostream &o, const Symbol::Flags &flags) {
std::size_t n{flags.count()}; std::size_t n{flags.count()};
std::size_t seen{0}; std::size_t seen{0};
for (std::size_t j{0}; seen < n; ++j) { for (std::size_t j{0}; seen < n; ++j) {
@ -472,7 +478,7 @@ std::ostream &operator<<(std::ostream &o, const Symbol::Flags &flags) {
return o; return o;
} }
std::ostream &operator<<(std::ostream &os, const Symbol &symbol) { llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Symbol &symbol) {
os << symbol.name(); os << symbol.name();
if (!symbol.attrs().empty()) { if (!symbol.attrs().empty()) {
os << ", " << symbol.attrs(); os << ", " << symbol.attrs();
@ -487,7 +493,7 @@ std::ostream &operator<<(std::ostream &os, const Symbol &symbol) {
// Output a unique name for a scope by qualifying it with the names of // Output a unique name for a scope by qualifying it with the names of
// parent scopes. For scopes without corresponding symbols, use the kind // parent scopes. For scopes without corresponding symbols, use the kind
// with an index (e.g. Block1, Block2, etc.). // with an index (e.g. Block1, Block2, etc.).
static void DumpUniqueName(std::ostream &os, const Scope &scope) { static void DumpUniqueName(llvm::raw_ostream &os, const Scope &scope) {
if (!scope.IsGlobal()) { if (!scope.IsGlobal()) {
DumpUniqueName(os, scope.parent()); DumpUniqueName(os, scope.parent());
os << '/'; os << '/';
@ -511,8 +517,8 @@ static void DumpUniqueName(std::ostream &os, const Scope &scope) {
// Dump a symbol for UnparseWithSymbols. This will be used for tests so the // Dump a symbol for UnparseWithSymbols. This will be used for tests so the
// format should be reasonably stable. // format should be reasonably stable.
std::ostream &DumpForUnparse( llvm::raw_ostream &DumpForUnparse(
std::ostream &os, const Symbol &symbol, bool isDef) { llvm::raw_ostream &os, const Symbol &symbol, bool isDef) {
DumpUniqueName(os, symbol.owner()); DumpUniqueName(os, symbol.owner());
os << '/' << symbol.name(); os << '/' << symbol.name();
if (isDef) { if (isDef) {

View File

@ -17,9 +17,9 @@
#include "flang/Semantics/symbol.h" #include "flang/Semantics/symbol.h"
#include "flang/Semantics/tools.h" #include "flang/Semantics/tools.h"
#include "flang/Semantics/type.h" #include "flang/Semantics/type.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm> #include <algorithm>
#include <set> #include <set>
#include <sstream>
#include <variant> #include <variant>
namespace Fortran::semantics { namespace Fortran::semantics {
@ -414,7 +414,8 @@ bool ExprTypeKindIsDefault(
// If an analyzed expr or assignment is missing, dump the node and die. // If an analyzed expr or assignment is missing, dump the node and die.
template<typename T> static void CheckMissingAnalysis(bool absent, const T &x) { template<typename T> static void CheckMissingAnalysis(bool absent, const T &x) {
if (absent) { if (absent) {
std::ostringstream ss; std::string buf;
llvm::raw_string_ostream ss{buf};
ss << "node has not been analyzed:\n"; ss << "node has not been analyzed:\n";
parser::DumpTree(ss, x); parser::DumpTree(ss, x);
common::die(ss.str().c_str()); common::die(ss.str().c_str());

View File

@ -12,8 +12,7 @@
#include "flang/Semantics/scope.h" #include "flang/Semantics/scope.h"
#include "flang/Semantics/symbol.h" #include "flang/Semantics/symbol.h"
#include "flang/Semantics/tools.h" #include "flang/Semantics/tools.h"
#include <ostream> #include "llvm/Support/raw_ostream.h"
#include <sstream>
namespace Fortran::semantics { namespace Fortran::semantics {
@ -272,7 +271,8 @@ void DerivedTypeSpec::Instantiate(
} }
std::string DerivedTypeSpec::AsFortran() const { std::string DerivedTypeSpec::AsFortran() const {
std::stringstream ss; std::string buf;
llvm::raw_string_ostream ss{buf};
ss << name_; ss << name_;
if (!rawParameters_.empty()) { if (!rawParameters_.empty()) {
CHECK(parameters_.empty()); CHECK(parameters_.empty());
@ -306,13 +306,13 @@ std::string DerivedTypeSpec::AsFortran() const {
return ss.str(); return ss.str();
} }
std::ostream &operator<<(std::ostream &o, const DerivedTypeSpec &x) { llvm::raw_ostream &operator<<(llvm::raw_ostream &o, const DerivedTypeSpec &x) {
return o << x.AsFortran(); return o << x.AsFortran();
} }
Bound::Bound(int bound) : expr_{bound} {} Bound::Bound(int bound) : expr_{bound} {}
std::ostream &operator<<(std::ostream &o, const Bound &x) { llvm::raw_ostream &operator<<(llvm::raw_ostream &o, const Bound &x) {
if (x.isAssumed()) { if (x.isAssumed()) {
o << '*'; o << '*';
} else if (x.isDeferred()) { } else if (x.isDeferred()) {
@ -325,7 +325,7 @@ std::ostream &operator<<(std::ostream &o, const Bound &x) {
return o; return o;
} }
std::ostream &operator<<(std::ostream &o, const ShapeSpec &x) { llvm::raw_ostream &operator<<(llvm::raw_ostream &o, const ShapeSpec &x) {
if (x.lb_.isAssumed()) { if (x.lb_.isAssumed()) {
CHECK(x.ub_.isAssumed()); CHECK(x.ub_.isAssumed());
o << ".."; o << "..";
@ -365,7 +365,8 @@ bool ArraySpec::IsAssumedRank() const {
return Rank() == 1 && front().lbound().isAssumed(); return Rank() == 1 && front().lbound().isAssumed();
} }
std::ostream &operator<<(std::ostream &os, const ArraySpec &arraySpec) { llvm::raw_ostream &operator<<(
llvm::raw_ostream &os, const ArraySpec &arraySpec) {
char sep{'('}; char sep{'('};
for (auto &shape : arraySpec) { for (auto &shape : arraySpec) {
os << sep << shape; os << sep << shape;
@ -398,7 +399,8 @@ std::string ParamValue::AsFortran() const {
case Category::Deferred: return ":"; case Category::Deferred: return ":";
case Category::Explicit: case Category::Explicit:
if (expr_) { if (expr_) {
std::stringstream ss; std::string buf;
llvm::raw_string_ostream ss{buf};
expr_->AsFortran(ss); expr_->AsFortran(ss);
return ss.str(); return ss.str();
} else { } else {
@ -407,7 +409,7 @@ std::string ParamValue::AsFortran() const {
} }
} }
std::ostream &operator<<(std::ostream &o, const ParamValue &x) { llvm::raw_ostream &operator<<(llvm::raw_ostream &o, const ParamValue &x) {
return o << x.AsFortran(); return o << x.AsFortran();
} }
@ -417,7 +419,8 @@ IntrinsicTypeSpec::IntrinsicTypeSpec(TypeCategory category, KindExpr &&kind)
} }
static std::string KindAsFortran(const KindExpr &kind) { static std::string KindAsFortran(const KindExpr &kind) {
std::stringstream ss; std::string buf;
llvm::raw_string_ostream ss{buf};
if (auto k{evaluate::ToInt64(kind)}) { if (auto k{evaluate::ToInt64(kind)}) {
ss << *k; // emit unsuffixed kind code ss << *k; // emit unsuffixed kind code
} else { } else {
@ -431,7 +434,8 @@ std::string IntrinsicTypeSpec::AsFortran() const {
KindAsFortran(kind_) + ')'; KindAsFortran(kind_) + ')';
} }
std::ostream &operator<<(std::ostream &os, const IntrinsicTypeSpec &x) { llvm::raw_ostream &operator<<(
llvm::raw_ostream &os, const IntrinsicTypeSpec &x) {
return os << x.AsFortran(); return os << x.AsFortran();
} }
@ -439,7 +443,8 @@ std::string CharacterTypeSpec::AsFortran() const {
return "CHARACTER(" + length_.AsFortran() + ',' + KindAsFortran(kind()) + ')'; return "CHARACTER(" + length_.AsFortran() + ',' + KindAsFortran(kind()) + ')';
} }
std::ostream &operator<<(std::ostream &os, const CharacterTypeSpec &x) { llvm::raw_ostream &operator<<(
llvm::raw_ostream &os, const CharacterTypeSpec &x) {
return os << x.AsFortran(); return os << x.AsFortran();
} }
@ -494,7 +499,7 @@ std::string DeclTypeSpec::AsFortran() const {
} }
} }
std::ostream &operator<<(std::ostream &o, const DeclTypeSpec &x) { llvm::raw_ostream &operator<<(llvm::raw_ostream &o, const DeclTypeSpec &x) {
return o << x.AsFortran(); return o << x.AsFortran();
} }

View File

@ -11,8 +11,8 @@
#include "flang/Parser/parse-tree.h" #include "flang/Parser/parse-tree.h"
#include "flang/Parser/unparse.h" #include "flang/Parser/unparse.h"
#include "flang/Semantics/symbol.h" #include "flang/Semantics/symbol.h"
#include "llvm/Support/raw_ostream.h"
#include <map> #include <map>
#include <ostream>
#include <set> #include <set>
namespace Fortran::semantics { namespace Fortran::semantics {
@ -24,7 +24,7 @@ namespace Fortran::semantics {
class SymbolDumpVisitor { class SymbolDumpVisitor {
public: public:
// Write out symbols referenced at this statement. // Write out symbols referenced at this statement.
void PrintSymbols(const parser::CharBlock &, std::ostream &, int); void PrintSymbols(const parser::CharBlock &, llvm::raw_ostream &, int);
template<typename T> bool Pre(const T &) { return true; } template<typename T> bool Pre(const T &) { return true; }
template<typename T> void Post(const T &) {} template<typename T> void Post(const T &) {}
@ -51,11 +51,11 @@ private:
std::optional<SourceName> currStmt_; // current statement we are processing std::optional<SourceName> currStmt_; // current statement we are processing
std::multimap<const char *, const Symbol *> symbols_; // location to symbol std::multimap<const char *, const Symbol *> symbols_; // location to symbol
std::set<const Symbol *> symbolsDefined_; // symbols that have been processed std::set<const Symbol *> symbolsDefined_; // symbols that have been processed
void Indent(std::ostream &, int) const; void Indent(llvm::raw_ostream &, int) const;
}; };
void SymbolDumpVisitor::PrintSymbols( void SymbolDumpVisitor::PrintSymbols(
const parser::CharBlock &location, std::ostream &out, int indent) { const parser::CharBlock &location, llvm::raw_ostream &out, int indent) {
std::set<const Symbol *> done; // prevent duplicates on this line std::set<const Symbol *> done; // prevent duplicates on this line
auto range{symbols_.equal_range(location.begin())}; auto range{symbols_.equal_range(location.begin())};
for (auto it{range.first}; it != range.second; ++it) { for (auto it{range.first}; it != range.second; ++it) {
@ -70,7 +70,7 @@ void SymbolDumpVisitor::PrintSymbols(
} }
} }
void SymbolDumpVisitor::Indent(std::ostream &out, int indent) const { void SymbolDumpVisitor::Indent(llvm::raw_ostream &out, int indent) const {
for (int i{0}; i < indent; ++i) { for (int i{0}; i < indent; ++i) {
out << ' '; out << ' ';
} }
@ -84,14 +84,13 @@ void SymbolDumpVisitor::Post(const parser::Name &name) {
} }
} }
void UnparseWithSymbols(std::ostream &out, const parser::Program &program, void UnparseWithSymbols(llvm::raw_ostream &out, const parser::Program &program,
parser::Encoding encoding) { parser::Encoding encoding) {
SymbolDumpVisitor visitor; SymbolDumpVisitor visitor;
parser::Walk(program, visitor); parser::Walk(program, visitor);
parser::preStatementType preStatement{ parser::preStatementType preStatement{
[&](const parser::CharBlock &location, std::ostream &out, int indent) { [&](const parser::CharBlock &location, llvm::raw_ostream &out,
visitor.PrintSymbols(location, out, indent); int indent) { visitor.PrintSymbols(location, out, indent); }};
}};
parser::Unparse(out, program, encoding, false, true, &preStatement); parser::Unparse(out, program, encoding, false, true, &preStatement);
} }
} }

View File

@ -8,26 +8,26 @@
// This file defines Dump routines available for calling from the debugger. // This file defines Dump routines available for calling from the debugger.
// Each is based on operator<< for that type. There are overloadings for // 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 #ifdef DEBUGF18
#include <iostream> #include "llvm/Support/raw_ostream.h"
#define DEFINE_DUMP(ns, name) \ #define DEFINE_DUMP(ns, name) \
namespace ns { \ namespace ns { \
class name; \ 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(llvm::raw_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) { \
if (x == nullptr) \ if (x == nullptr) \
os << "null\n"; \ os << "null\n"; \
else \ else \
Dump(os, *x); \ Dump(os, *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(std::cerr, *x); } void Dump(const ns::name *x) { Dump(llvm::errs(), *x); }
namespace Fortran { namespace Fortran {
DEFINE_DUMP(parser, Name) DEFINE_DUMP(parser, Name)

View File

@ -31,11 +31,12 @@
#include "flang/Parser/parsing.h" #include "flang/Parser/parsing.h"
#include "flang/Parser/provenance.h" #include "flang/Parser/provenance.h"
#include "flang/Parser/unparse.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 <cstdio>
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <iostream>
#include <list> #include <list>
#include <memory> #include <memory>
#include <optional> #include <optional>
@ -112,14 +113,14 @@ bool ParentProcess() {
void Exec(std::vector<char *> &argv, bool verbose = false) { void Exec(std::vector<char *> &argv, bool verbose = false) {
if (verbose) { if (verbose) {
for (size_t j{0}; j < argv.size(); ++j) { 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); argv.push_back(nullptr);
execvp(argv[0], &argv[0]); execvp(argv[0], &argv[0]);
std::cerr << "execvp(" << argv[0] << ") failed: " << std::strerror(errno) llvm::errs() << "execvp(" << argv[0]
<< '\n'; << ") failed: " << llvm::sys::StrError(errno) << '\n';
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -173,52 +174,52 @@ std::string CompileFortran(
parsing.Prescan(path, options); parsing.Prescan(path, options);
if (!parsing.messages().empty() && if (!parsing.messages().empty() &&
(driver.warningsAreErrors || parsing.messages().AnyFatalError())) { (driver.warningsAreErrors || parsing.messages().AnyFatalError())) {
std::cerr << driver.prefix << "could not scan " << path << '\n'; llvm::errs() << driver.prefix << "could not scan " << path << '\n';
parsing.messages().Emit(std::cerr, parsing.cooked()); parsing.messages().Emit(llvm::errs(), parsing.cooked());
exitStatus = EXIT_FAILURE; exitStatus = EXIT_FAILURE;
return {}; return {};
} }
if (driver.dumpProvenance) { if (driver.dumpProvenance) {
parsing.DumpProvenance(std::cout); parsing.DumpProvenance(llvm::outs());
return {}; return {};
} }
if (driver.dumpCookedChars) { if (driver.dumpCookedChars) {
parsing.DumpCookedChars(std::cout); parsing.DumpCookedChars(llvm::outs());
return {}; return {};
} }
parsing.Parse(&std::cout); parsing.Parse(llvm::outs());
auto stop{CPUseconds()}; auto stop{CPUseconds()};
if (driver.timeParse) { if (driver.timeParse) {
if (canTime) { if (canTime) {
std::cout << "parse time for " << path << ": " << (stop - start) llvm::outs() << "parse time for " << path << ": " << (stop - start)
<< " CPU seconds\n"; << " CPU seconds\n";
} else { } 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.ClearLog();
parsing.messages().Emit(std::cerr, parsing.cooked()); parsing.messages().Emit(llvm::errs(), parsing.cooked());
if (!parsing.consumedWholeFile()) { if (!parsing.consumedWholeFile()) {
parsing.EmitMessage( parsing.EmitMessage(llvm::errs(), parsing.finalRestingPlace(),
std::cerr, parsing.finalRestingPlace(), "parser FAIL (final position)"); "parser FAIL (final position)");
exitStatus = EXIT_FAILURE; exitStatus = EXIT_FAILURE;
return {}; return {};
} }
if ((!parsing.messages().empty() && if ((!parsing.messages().empty() &&
(driver.warningsAreErrors || parsing.messages().AnyFatalError())) || (driver.warningsAreErrors || parsing.messages().AnyFatalError())) ||
!parsing.parseTree()) { !parsing.parseTree()) {
std::cerr << driver.prefix << "could not parse " << path << '\n'; llvm::errs() << driver.prefix << "could not parse " << path << '\n';
exitStatus = EXIT_FAILURE; exitStatus = EXIT_FAILURE;
return {}; return {};
} }
auto &parseTree{*parsing.parseTree()}; auto &parseTree{*parsing.parseTree()};
if (driver.dumpParseTree) { if (driver.dumpParseTree) {
Fortran::parser::DumpTree(std::cout, parseTree); Fortran::parser::DumpTree(llvm::outs(), parseTree);
return {}; return {};
} }
if (driver.dumpUnparse) { if (driver.dumpUnparse) {
Unparse(std::cout, parseTree, driver.encoding, true /*capitalize*/, Unparse(llvm::outs(), parseTree, driver.encoding, true /*capitalize*/,
options.features.IsEnabled( options.features.IsEnabled(
Fortran::common::LanguageFeature::BackslashEscapes)); Fortran::common::LanguageFeature::BackslashEscapes));
return {}; return {};
@ -233,8 +234,12 @@ std::string CompileFortran(
std::snprintf(tmpSourcePath, sizeof tmpSourcePath, "/tmp/f18-%lx.f90", std::snprintf(tmpSourcePath, sizeof tmpSourcePath, "/tmp/f18-%lx.f90",
static_cast<unsigned long>(getpid())); static_cast<unsigned long>(getpid()));
{ {
std::ofstream tmpSource; std::error_code EC;
tmpSource.open(tmpSourcePath); 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*/, Unparse(tmpSource, parseTree, driver.encoding, true /*capitalize*/,
options.features.IsEnabled( options.features.IsEnabled(
Fortran::common::LanguageFeature::BackslashEscapes)); Fortran::common::LanguageFeature::BackslashEscapes));
@ -402,7 +407,7 @@ int main(int argc, char *const argv[]) {
} else if (arg == "-i8" || arg == "-fdefault-integer-8") { } else if (arg == "-i8" || arg == "-fdefault-integer-8") {
defaultKinds.set_defaultIntegerKind(8); defaultKinds.set_defaultIntegerKind(8);
} else if (arg == "-help" || arg == "--help" || arg == "-?") { } else if (arg == "-help" || arg == "--help" || arg == "-?") {
std::cerr llvm::errs()
<< "f18-parse-demo options:\n" << "f18-parse-demo options:\n"
<< " -Mfixed | -Mfree force the source form\n" << " -Mfixed | -Mfree force the source form\n"
<< " -Mextend 132-column fixed form\n" << " -Mextend 132-column fixed form\n"
@ -425,7 +430,7 @@ int main(int argc, char *const argv[]) {
<< "Other options are passed through to the $F18_FC compiler.\n"; << "Other options are passed through to the $F18_FC compiler.\n";
return exitStatus; return exitStatus;
} else if (arg == "-V") { } else if (arg == "-V") {
std::cerr << "\nf18-parse-demo\n"; llvm::errs() << "\nf18-parse-demo\n";
return exitStatus; return exitStatus;
} else { } else {
driver.fcArgs.push_back(arg); driver.fcArgs.push_back(arg);

View File

@ -23,12 +23,12 @@
#include "flang/Semantics/expression.h" #include "flang/Semantics/expression.h"
#include "flang/Semantics/semantics.h" #include "flang/Semantics/semantics.h"
#include "flang/Semantics/unparse-with-symbols.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 "llvm/Support/raw_ostream.h"
#include <cerrno>
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <iostream>
#include <list> #include <list>
#include <memory> #include <memory>
#include <optional> #include <optional>
@ -58,8 +58,9 @@ struct MeasurementVisitor {
void MeasureParseTree(const Fortran::parser::Program &program) { void MeasureParseTree(const Fortran::parser::Program &program) {
MeasurementVisitor visitor; MeasurementVisitor visitor;
Fortran::parser::Walk(program, visitor); Fortran::parser::Walk(program, visitor);
std::cout << "Parse tree comprises " << visitor.objects llvm::outs() << "Parse tree comprises " << visitor.objects
<< " objects and occupies " << visitor.bytes << " total bytes.\n"; << " objects and occupies " << visitor.bytes
<< " total bytes.\n";
} }
std::vector<std::string> filesToDelete; std::vector<std::string> filesToDelete;
@ -123,14 +124,14 @@ bool ParentProcess() {
void Exec(std::vector<char *> &argv, bool verbose = false) { void Exec(std::vector<char *> &argv, bool verbose = false) {
if (verbose) { if (verbose) {
for (size_t j{0}; j < argv.size(); ++j) { 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); argv.push_back(nullptr);
execvp(argv[0], &argv[0]); execvp(argv[0], &argv[0]);
std::cerr << "execvp(" << argv[0] << ") failed: " << std::strerror(errno) llvm::errs() << "execvp(" << argv[0]
<< '\n'; << ") failed: " << llvm::sys::StrError(errno) << '\n';
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -168,21 +169,22 @@ std::string RelocatableName(const DriverOptions &driver, std::string path) {
int exitStatus{EXIT_SUCCESS}; int exitStatus{EXIT_SUCCESS};
static Fortran::parser::AnalyzedObjectsAsFortran asFortran{ 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) { if (x.v) {
x.v->AsFortran(o); x.v->AsFortran(o);
} else { } else {
o << "(bad expression)"; o << "(bad expression)";
} }
}, },
[](std::ostream &o, const Fortran::evaluate::GenericAssignmentWrapper &x) { [](llvm::raw_ostream &o,
const Fortran::evaluate::GenericAssignmentWrapper &x) {
if (x.v) { if (x.v) {
x.v->AsFortran(o); x.v->AsFortran(o);
} else { } else {
o << "(bad assignment)"; o << "(bad assignment)";
} }
}, },
[](std::ostream &o, const Fortran::evaluate::ProcedureRef &x) { [](llvm::raw_ostream &o, const Fortran::evaluate::ProcedureRef &x) {
x.AsFortran(o << "CALL "); x.AsFortran(o << "CALL ");
}, },
}; };
@ -211,37 +213,37 @@ std::string CompileFortran(std::string path, Fortran::parser::Options options,
parsing.Prescan(path, options); parsing.Prescan(path, options);
if (!parsing.messages().empty() && if (!parsing.messages().empty() &&
(driver.warningsAreErrors || parsing.messages().AnyFatalError())) { (driver.warningsAreErrors || parsing.messages().AnyFatalError())) {
std::cerr << driver.prefix << "could not scan " << path << '\n'; llvm::errs() << driver.prefix << "could not scan " << path << '\n';
parsing.messages().Emit(std::cerr, parsing.cooked()); parsing.messages().Emit(llvm::errs(), parsing.cooked());
exitStatus = EXIT_FAILURE; exitStatus = EXIT_FAILURE;
return {}; return {};
} }
if (driver.dumpProvenance) { if (driver.dumpProvenance) {
parsing.DumpProvenance(std::cout); parsing.DumpProvenance(llvm::outs());
return {}; return {};
} }
if (driver.dumpCookedChars) { if (driver.dumpCookedChars) {
parsing.messages().Emit(std::cerr, parsing.cooked()); parsing.messages().Emit(llvm::errs(), parsing.cooked());
parsing.DumpCookedChars(std::cout); parsing.DumpCookedChars(llvm::outs());
return {}; return {};
} }
parsing.Parse(&std::cout); parsing.Parse(llvm::outs());
if (options.instrumentedParse) { if (options.instrumentedParse) {
parsing.DumpParsingLog(std::cout); parsing.DumpParsingLog(llvm::outs());
return {}; return {};
} }
parsing.ClearLog(); parsing.ClearLog();
parsing.messages().Emit(std::cerr, parsing.cooked()); parsing.messages().Emit(llvm::errs(), parsing.cooked());
if (!parsing.consumedWholeFile()) { if (!parsing.consumedWholeFile()) {
parsing.EmitMessage( parsing.EmitMessage(llvm::errs(), parsing.finalRestingPlace(),
std::cerr, parsing.finalRestingPlace(), "parser FAIL (final position)"); "parser FAIL (final position)");
exitStatus = EXIT_FAILURE; exitStatus = EXIT_FAILURE;
return {}; return {};
} }
if ((!parsing.messages().empty() && if ((!parsing.messages().empty() &&
(driver.warningsAreErrors || parsing.messages().AnyFatalError())) || (driver.warningsAreErrors || parsing.messages().AnyFatalError())) ||
!parsing.parseTree()) { !parsing.parseTree()) {
std::cerr << driver.prefix << "could not parse " << path << '\n'; llvm::errs() << driver.prefix << "could not parse " << path << '\n';
exitStatus = EXIT_FAILURE; exitStatus = EXIT_FAILURE;
return {}; return {};
} }
@ -255,25 +257,25 @@ std::string CompileFortran(std::string path, Fortran::parser::Options options,
Fortran::semantics::Semantics semantics{semanticsContext, parseTree, Fortran::semantics::Semantics semantics{semanticsContext, parseTree,
parsing.cooked(), driver.debugModuleWriter}; parsing.cooked(), driver.debugModuleWriter};
semantics.Perform(); semantics.Perform();
semantics.EmitMessages(std::cerr); semantics.EmitMessages(llvm::errs());
if (driver.dumpSymbols) { if (driver.dumpSymbols) {
semantics.DumpSymbols(std::cout); semantics.DumpSymbols(llvm::outs());
} }
if (semantics.AnyFatalError()) { if (semantics.AnyFatalError()) {
std::cerr << driver.prefix << "semantic errors in " << path << '\n'; llvm::errs() << driver.prefix << "semantic errors in " << path << '\n';
exitStatus = EXIT_FAILURE; exitStatus = EXIT_FAILURE;
if (driver.dumpParseTree) { if (driver.dumpParseTree) {
Fortran::parser::DumpTree(std::cout, parseTree, &asFortran); Fortran::parser::DumpTree(llvm::outs(), parseTree, &asFortran);
} }
return {}; return {};
} }
if (driver.dumpUnparseWithSymbols) { if (driver.dumpUnparseWithSymbols) {
Fortran::semantics::UnparseWithSymbols( Fortran::semantics::UnparseWithSymbols(
std::cout, parseTree, driver.encoding); llvm::outs(), parseTree, driver.encoding);
return {}; return {};
} }
if (driver.getSymbolsSources) { if (driver.getSymbolsSources) {
semantics.DumpSymbolsSources(std::cout); semantics.DumpSymbolsSources(llvm::outs());
return {}; return {};
} }
if (driver.getDefinition) { if (driver.getDefinition) {
@ -281,13 +283,13 @@ std::string CompileFortran(std::string path, Fortran::parser::Options options,
driver.getDefinitionArgs.line, driver.getDefinitionArgs.line,
driver.getDefinitionArgs.startColumn, driver.getDefinitionArgs.startColumn,
driver.getDefinitionArgs.endColumn)}) { 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)}) { if (auto symbol{semanticsContext.FindScope(*cb).FindSymbol(*cb)}) {
std::cerr << "Found symbol name: " << symbol->name().ToString() llvm::errs() << "Found symbol name: " << symbol->name().ToString()
<< "\n"; << "\n";
if (auto sourceInfo{ if (auto sourceInfo{
parsing.cooked().GetSourcePositionRange(symbol->name())}) { parsing.cooked().GetSourcePositionRange(symbol->name())}) {
std::cout << symbol->name().ToString() << ": " llvm::outs() << symbol->name().ToString() << ": "
<< sourceInfo->first.file.path() << ", " << sourceInfo->first.file.path() << ", "
<< sourceInfo->first.line << ", " << sourceInfo->first.line << ", "
<< sourceInfo->first.column << "-" << sourceInfo->first.column << "-"
@ -297,16 +299,16 @@ std::string CompileFortran(std::string path, Fortran::parser::Options options,
} }
} }
} }
std::cerr << "Symbol not found.\n"; llvm::errs() << "Symbol not found.\n";
exitStatus = EXIT_FAILURE; exitStatus = EXIT_FAILURE;
return {}; return {};
} }
} }
if (driver.dumpParseTree) { if (driver.dumpParseTree) {
Fortran::parser::DumpTree(std::cout, parseTree, &asFortran); Fortran::parser::DumpTree(llvm::outs(), parseTree, &asFortran);
} }
if (driver.dumpUnparse) { if (driver.dumpUnparse) {
Unparse(std::cout, parseTree, driver.encoding, true /*capitalize*/, Unparse(llvm::outs(), parseTree, driver.encoding, true /*capitalize*/,
options.features.IsEnabled( options.features.IsEnabled(
Fortran::common::LanguageFeature::BackslashEscapes), Fortran::common::LanguageFeature::BackslashEscapes),
nullptr /* action before each statement */, &asFortran); nullptr /* action before each statement */, &asFortran);
@ -317,7 +319,7 @@ std::string CompileFortran(std::string path, Fortran::parser::Options options,
Fortran::lower::annotateControl(*ast); Fortran::lower::annotateControl(*ast);
Fortran::lower::dumpPFT(llvm::outs(), *ast); Fortran::lower::dumpPFT(llvm::outs(), *ast);
} else { } else {
std::cerr << "Pre FIR Tree is NULL.\n"; llvm::errs() << "Pre FIR Tree is NULL.\n";
exitStatus = EXIT_FAILURE; exitStatus = EXIT_FAILURE;
} }
} }
@ -331,8 +333,12 @@ std::string CompileFortran(std::string path, Fortran::parser::Options options,
std::snprintf(tmpSourcePath, sizeof tmpSourcePath, "/tmp/f18-%lx.f90", std::snprintf(tmpSourcePath, sizeof tmpSourcePath, "/tmp/f18-%lx.f90",
static_cast<unsigned long>(getpid())); static_cast<unsigned long>(getpid()));
{ {
std::ofstream tmpSource; std::error_code EC;
tmpSource.open(tmpSourcePath); 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*/, Unparse(tmpSource, parseTree, driver.encoding, true /*capitalize*/,
options.features.IsEnabled( options.features.IsEnabled(
Fortran::common::LanguageFeature::BackslashEscapes), Fortran::common::LanguageFeature::BackslashEscapes),
@ -558,13 +564,13 @@ int main(int argc, char *const argv[]) {
int arguments[3]; int arguments[3];
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
if (args.empty()) { 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; return EXIT_FAILURE;
} }
arguments[i] = std::strtol(args.front().c_str(), &endptr, 10); arguments[i] = std::strtol(args.front().c_str(), &endptr, 10);
if (*endptr != '\0') { if (*endptr != '\0') {
std::cerr << "Invalid argument to -fget-definitions: " << args.front() llvm::errs() << "Invalid argument to -fget-definitions: "
<< '\n'; << args.front() << '\n';
return EXIT_FAILURE; return EXIT_FAILURE;
} }
args.pop_front(); args.pop_front();
@ -573,7 +579,7 @@ int main(int argc, char *const argv[]) {
} else if (arg == "-fget-symbols-sources") { } else if (arg == "-fget-symbols-sources") {
driver.getSymbolsSources = true; driver.getSymbolsSources = true;
} else if (arg == "-help" || arg == "--help" || arg == "-?") { } else if (arg == "-help" || arg == "--help" || arg == "-?") {
std::cerr llvm::errs()
<< "f18 options:\n" << "f18 options:\n"
<< " -Mfixed | -Mfree force the source form\n" << " -Mfixed | -Mfree force the source form\n"
<< " -Mextend 132-column fixed form\n" << " -Mextend 132-column fixed form\n"
@ -608,7 +614,7 @@ int main(int argc, char *const argv[]) {
<< "Other options are passed through to the compiler.\n"; << "Other options are passed through to the compiler.\n";
return exitStatus; return exitStatus;
} else if (arg == "-V") { } else if (arg == "-V") {
std::cerr << "\nf18 compiler (under development)\n"; llvm::errs() << "\nf18 compiler (under development)\n";
return exitStatus; return exitStatus;
} else { } else {
driver.pgf90Args.push_back(arg); driver.pgf90Args.push_back(arg);

View File

@ -12,6 +12,7 @@ add_executable(quick-sanity-test
target_link_libraries(quick-sanity-test target_link_libraries(quick-sanity-test
FortranDecimal FortranDecimal
LLVMSupport
) )
add_executable(thorough-test add_executable(thorough-test
@ -20,6 +21,7 @@ add_executable(thorough-test
target_link_libraries(thorough-test target_link_libraries(thorough-test
FortranDecimal FortranDecimal
LLVMSupport
) )
add_test(Sanity quick-sanity-test) add_test(Sanity quick-sanity-test)

View File

@ -1,8 +1,8 @@
#include "flang/Decimal/decimal.h" #include "flang/Decimal/decimal.h"
#include "llvm/Support/raw_ostream.h"
#include <cinttypes> #include <cinttypes>
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <iostream>
using namespace Fortran::decimal; using namespace Fortran::decimal;
@ -14,11 +14,12 @@ union u {
std::uint32_t u; std::uint32_t u;
}; };
std::ostream &failed(float x) { llvm::raw_ostream &failed(float x) {
++fails; ++fails;
union u u; union u u;
u.x = x; 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) { void testDirect(float x, const char *expect, int expectExpo, int flags = 0) {
@ -60,15 +61,13 @@ void testReadback(float x, int flags) {
if (!(x == x)) { if (!(x == x)) {
if (y == y || *p != '\0' || (rflags & Invalid)) { if (y == y || *p != '\0' || (rflags & Invalid)) {
u.x = y; u.x = y;
failed(x) << " (NaN) " << flags << ": -> '" << result.str << "' -> 0x" failed(x) << " (NaN) " << flags << ": -> '" << result.str << "' -> 0x";
<< std::hex << u.u << std::dec << " '" << p << "' " << rflags failed(x).write_hex(u.u) << " '" << p << "' " << rflags << '\n';
<< '\n';
} }
} else if (x != y || *p != '\0' || (rflags & Invalid)) { } else if (x != y || *p != '\0' || (rflags & Invalid)) {
u.x = y; u.x = y;
failed(x) << ' ' << flags << ": -> '" << result.str << "' -> 0x" failed(x) << ' ' << flags << ": -> '" << result.str << "' -> 0x";
<< std::hex << u.u << std::dec << " '" << p << "' " << rflags failed(x).write_hex(u.u) << " '" << p << "' " << rflags << '\n';
<< '\n';
} }
} }
} }
@ -138,6 +137,6 @@ int main() {
testReadback(u.x, Minimize); testReadback(u.x, Minimize);
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; return fails > 0;
} }

View File

@ -1,8 +1,8 @@
#include "flang/Decimal/decimal.h" #include "flang/Decimal/decimal.h"
#include "llvm/Support/raw_ostream.h"
#include <cinttypes> #include <cinttypes>
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <iostream>
static constexpr int incr{1}; // steps through all values static constexpr int incr{1}; // steps through all values
static constexpr bool doNegative{true}; static constexpr bool doNegative{true};
@ -18,11 +18,12 @@ union u {
std::uint32_t u; std::uint32_t u;
}; };
std::ostream &failed(float x) { llvm::raw_ostream &failed(float x) {
++fails; ++fails;
union u u; union u u;
u.x = x; 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) { void testReadback(float x, int flags) {
@ -30,9 +31,10 @@ void testReadback(float x, int flags) {
union u u; union u u;
u.x = x; u.x = x;
if (!(tests & 0x3fffff)) { 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)) { } else if (!(tests & 0xffff)) {
std::cerr << '.'; llvm::errs() << '.';
} }
++tests; ++tests;
auto result{ConvertFloatToDecimal(buffer, sizeof buffer, auto result{ConvertFloatToDecimal(buffer, sizeof buffer,
@ -56,15 +58,13 @@ void testReadback(float x, int flags) {
if (!(x == x)) { if (!(x == x)) {
if (y == y || *p != '\0' || (rflags & Invalid)) { if (y == y || *p != '\0' || (rflags & Invalid)) {
u.x = y; u.x = y;
failed(x) << " (NaN) " << flags << ": -> '" << result.str << "' -> 0x" failed(x) << " (NaN) " << flags << ": -> '" << result.str << "' -> 0x";
<< std::hex << u.u << std::dec << " '" << p << "' " << rflags failed(x).write_hex(u.u) << " '" << p << "' " << rflags << '\n';
<< '\n';
} }
} else if (x != y || *p != '\0' || (rflags & Invalid)) { } else if (x != y || *p != '\0' || (rflags & Invalid)) {
u.x = y; u.x = y;
failed(x) << ' ' << flags << ": -> '" << result.str << "' -> 0x" failed(x) << ' ' << flags << ": -> '" << result.str << "' -> 0x";
<< std::hex << u.u << std::dec << " '" << p << "' " << rflags failed(x).write_hex(u.u) << " '" << p << "' " << rflags << '\n';
<< '\n';
} }
} }
} }
@ -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; return fails > 0;
} }

View File

@ -17,6 +17,7 @@ add_executable(leading-zero-bit-count-test
target_link_libraries(leading-zero-bit-count-test target_link_libraries(leading-zero-bit-count-test
FortranEvaluateTesting FortranEvaluateTesting
LLVMSupport
) )
add_executable(bit-population-count-test add_executable(bit-population-count-test
@ -25,6 +26,7 @@ add_executable(bit-population-count-test
target_link_libraries(bit-population-count-test target_link_libraries(bit-population-count-test
FortranEvaluateTesting FortranEvaluateTesting
LLVMSupport
) )
add_executable(uint128-test add_executable(uint128-test
@ -33,6 +35,7 @@ add_executable(uint128-test
target_link_libraries(uint128-test target_link_libraries(uint128-test
FortranEvaluateTesting FortranEvaluateTesting
LLVMSupport
) )
# These routines live in lib/Common but we test them here. # These routines live in lib/Common but we test them here.
@ -49,6 +52,7 @@ target_link_libraries(expression-test
FortranEvaluate FortranEvaluate
FortranSemantics FortranSemantics
FortranParser FortranParser
LLVMSupport
) )
add_executable(integer-test add_executable(integer-test
@ -59,6 +63,7 @@ target_link_libraries(integer-test
FortranEvaluateTesting FortranEvaluateTesting
FortranEvaluate FortranEvaluate
FortranSemantics FortranSemantics
LLVMSupport
) )
add_executable(intrinsics-test add_executable(intrinsics-test
@ -72,6 +77,7 @@ target_link_libraries(intrinsics-test
FortranSemantics FortranSemantics
FortranParser FortranParser
FortranRuntime FortranRuntime
LLVMSupport
) )
add_executable(logical-test add_executable(logical-test
@ -82,6 +88,7 @@ target_link_libraries(logical-test
FortranEvaluateTesting FortranEvaluateTesting
FortranEvaluate FortranEvaluate
FortranSemantics FortranSemantics
LLVMSupport
) )
# GCC -fno-exceptions breaks the fenv.h interfaces needed to capture # GCC -fno-exceptions breaks the fenv.h interfaces needed to capture
@ -98,6 +105,7 @@ target_link_libraries(real-test
FortranEvaluate FortranEvaluate
FortranDecimal FortranDecimal
FortranSemantics FortranSemantics
LLVMSupport
) )
add_executable(reshape-test add_executable(reshape-test
@ -109,6 +117,7 @@ target_link_libraries(reshape-test
FortranSemantics FortranSemantics
FortranEvaluate FortranEvaluate
FortranRuntime FortranRuntime
LLVMSupport
) )
add_executable(ISO-Fortran-binding-test add_executable(ISO-Fortran-binding-test
@ -120,6 +129,7 @@ target_link_libraries(ISO-Fortran-binding-test
FortranEvaluate FortranEvaluate
FortranSemantics FortranSemantics
FortranRuntime FortranRuntime
LLVMSupport
) )
add_executable(folding-test add_executable(folding-test
@ -130,6 +140,7 @@ target_link_libraries(folding-test
FortranEvaluateTesting FortranEvaluateTesting
FortranEvaluate FortranEvaluate
FortranSemantics FortranSemantics
LLVMSupport
) )
add_test(Expression expression-test) add_test(Expression expression-test)

View File

@ -1,10 +1,8 @@
#include "testing.h" #include "testing.h"
#include "../../include/flang/ISO_Fortran_binding.h" #include "../../include/flang/ISO_Fortran_binding.h"
#include "../../runtime/descriptor.h" #include "../../runtime/descriptor.h"
#include "llvm/Support/raw_ostream.h"
#include <type_traits> #include <type_traits>
#ifdef VERBOSE
#include <iostream>
#endif
using namespace Fortran::runtime; using namespace Fortran::runtime;
using namespace Fortran::ISO; using namespace Fortran::ISO;
@ -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, static void DumpTestWorld(const void *bAddr, CFI_attribute_t attr,
CFI_type_t ty, std::size_t eLen, CFI_rank_t rank, CFI_type_t ty, std::size_t eLen, CFI_rank_t rank,
const CFI_index_t *eAddr) { const CFI_index_t *eAddr) {
std::cout << " base_addr: " << std::hex llvm::outs() << " base_addr: ";
<< reinterpret_cast<std::intptr_t>(bAddr) llvm::outs().write_hex(reinterpret_cast<std::intptr_t>(bAddr))
<< " attribute: " << static_cast<int>(attr) << std::dec << " attribute: " << static_cast<int>(attr)
<< " type: " << static_cast<int>(ty) << " elem_len: " << eLen << " type: " << static_cast<int>(ty) << " elem_len: " << eLen
<< " rank: " << static_cast<int>(rank) << " extent: " << std::hex << " rank: " << static_cast<int>(rank) << " extent: ";
<< reinterpret_cast<std::intptr_t>(eAddr) << std::endl llvm::outs().write_hex(reinterpret_cast<std::intptr_t>(eAddr)) << '\n';
<< std::dec; llvm::outs().flush();
} }
#endif #endif

View File

@ -1,4 +1,5 @@
#include "fp-testing.h" #include "fp-testing.h"
#include "llvm/Support/Errno.h"
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
@ -15,11 +16,13 @@ ScopedHostFloatingPointEnvironment::ScopedHostFloatingPointEnvironment(
) { ) {
errno = 0; errno = 0;
if (feholdexcept(&originalFenv_) != 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(); std::abort();
} }
if (fegetenv(&currentFenv_) != 0) { 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(); std::abort();
} }
#if __x86_64__ #if __x86_64__
@ -38,7 +41,8 @@ ScopedHostFloatingPointEnvironment::ScopedHostFloatingPointEnvironment(
#endif #endif
errno = 0; errno = 0;
if (fesetenv(&currentFenv_) != 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(); std::abort();
} }
} }
@ -46,7 +50,8 @@ ScopedHostFloatingPointEnvironment::ScopedHostFloatingPointEnvironment(
ScopedHostFloatingPointEnvironment::~ScopedHostFloatingPointEnvironment() { ScopedHostFloatingPointEnvironment::~ScopedHostFloatingPointEnvironment() {
errno = 0; errno = 0;
if (fesetenv(&originalFenv_) != 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(); std::abort();
} }
} }

View File

@ -4,8 +4,8 @@
#include "flang/Evaluate/expression.h" #include "flang/Evaluate/expression.h"
#include "flang/Evaluate/tools.h" #include "flang/Evaluate/tools.h"
#include "flang/Parser/provenance.h" #include "flang/Parser/provenance.h"
#include "llvm/Support/raw_ostream.h"
#include <initializer_list> #include <initializer_list>
#include <iostream>
#include <map> #include <map>
#include <string> #include <string>
@ -31,7 +31,7 @@ public:
parser::ContextualMessages Messages(parser::Messages &buffer) { parser::ContextualMessages Messages(parser::Messages &buffer) {
return parser::ContextualMessages{cooked_.data(), &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_); messages.Emit(o, cooked_);
} }
@ -88,17 +88,18 @@ struct TestCall {
int rank = 0, bool isElemental = false) { int rank = 0, bool isElemental = false) {
Marshal(); Marshal();
parser::CharBlock fName{strings(name)}; parser::CharBlock fName{strings(name)};
std::cout << "function: " << fName.ToString(); llvm::outs() << "function: " << fName.ToString();
char sep{'('}; char sep{'('};
for (const auto &a : args) { for (const auto &a : args) {
std::cout << sep; llvm::outs() << sep;
sep = ','; sep = ',';
a->AsFortran(std::cout); a->AsFortran(llvm::outs());
} }
if (sep == '(') { if (sep == '(') {
std::cout << '('; llvm::outs() << '(';
} }
std::cout << ')' << std::endl; llvm::outs() << ')' << '\n';
llvm::outs().flush();
CallCharacteristics call{fName.ToString()}; CallCharacteristics call{fName.ToString()};
auto messages{strings.Messages(buffer)}; auto messages{strings.Messages(buffer)};
FoldingContext context{messages, defaults, table}; FoldingContext context{messages, defaults, table};
@ -126,7 +127,7 @@ struct TestCall {
TEST((messages.messages() && messages.messages()->AnyFatalError()) || TEST((messages.messages() && messages.messages()->AnyFatalError()) ||
name == "bad"); name == "bad");
} }
strings.Emit(std::cout, buffer); strings.Emit(llvm::outs(), buffer);
} }
const common::IntrinsicTypeDefaultKinds &defaults; const common::IntrinsicTypeDefaultKinds &defaults;
@ -143,7 +144,7 @@ void TestIntrinsics() {
MATCH(4, defaults.GetDefaultKind(TypeCategory::Integer)); MATCH(4, defaults.GetDefaultKind(TypeCategory::Integer));
MATCH(4, defaults.GetDefaultKind(TypeCategory::Real)); MATCH(4, defaults.GetDefaultKind(TypeCategory::Real));
IntrinsicProcTable table{IntrinsicProcTable::Configure(defaults)}; IntrinsicProcTable table{IntrinsicProcTable::Configure(defaults)};
table.Dump(std::cout); table.Dump(llvm::outs());
using Int1 = Type<TypeCategory::Integer, 1>; using Int1 = Type<TypeCategory::Integer, 1>;
using Int4 = Type<TypeCategory::Integer, 4>; using Int4 = Type<TypeCategory::Integer, 4>;

View File

@ -1,10 +1,10 @@
#include "fp-testing.h" #include "fp-testing.h"
#include "testing.h" #include "testing.h"
#include "flang/Evaluate/type.h" #include "flang/Evaluate/type.h"
#include "llvm/Support/raw_ostream.h"
#include <cmath> #include <cmath>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <sstream>
#include <type_traits> #include <type_traits>
using namespace Fortran::evaluate; using namespace Fortran::evaluate;
@ -149,7 +149,8 @@ template<typename R> void basicTests(int rm, Rounding rounding) {
TEST(ivf.flags.empty())(ldesc); TEST(ivf.flags.empty())(ldesc);
MATCH(x, ivf.value.ToUInt64())(ldesc); MATCH(x, ivf.value.ToUInt64())(ldesc);
if (rounding.mode == RoundingMode::TiesToEven) { // to match stold() 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*/); vr.value.AsFortran(ss, kind, false /*exact*/);
std::string decimal{ss.str()}; std::string decimal{ss.str()};
const char *p{decimal.data()}; const char *p{decimal.data()};
@ -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)); ("%d IsInfinite(0x%jx)", pass, static_cast<std::intmax_t>(rj));
static constexpr int kind{REAL::bits / 8}; 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*/); x.AsFortran(ss, kind, false /*exact*/);
std::string s{ss.str()}; std::string s{ss.str()};
if (IsNaN(rj)) { if (IsNaN(rj)) {

View File

@ -1,8 +1,8 @@
#include "testing.h" #include "testing.h"
#include "llvm/Support/raw_ostream.h"
#include <cstdarg> #include <cstdarg>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <iostream>
namespace testing { namespace testing {
@ -103,22 +103,22 @@ FailureDetailPrinter Compare(const char *file, int line, const char *xs,
int Complete() { int Complete() {
if (failures == 0) { if (failures == 0) {
if (passes == 1) { if (passes == 1) {
std::cout << "single test PASSES\n"; llvm::outs() << "single test PASSES\n";
} else { } else {
std::cout << "all " << std::dec << passes << " tests PASS\n"; llvm::outs() << "all " << passes << " tests PASS\n";
} }
passes = 0; passes = 0;
return EXIT_SUCCESS; return EXIT_SUCCESS;
} else { } else {
if (passes == 1) { if (passes == 1) {
std::cerr << "1 test passes, "; llvm::errs() << "1 test passes, ";
} else { } else {
std::cerr << std::dec << passes << " tests pass, "; llvm::errs() << passes << " tests pass, ";
} }
if (failures == 1) { if (failures == 1) {
std::cerr << "1 test FAILS\n"; llvm::errs() << "1 test FAILS\n";
} else { } else {
std::cerr << std::dec << failures << " tests FAIL\n"; llvm::errs() << failures << " tests FAIL\n";
} }
passes = failures = 0; passes = failures = 0;
return EXIT_FAILURE; return EXIT_FAILURE;

View File

@ -1,8 +1,8 @@
#define AVOID_NATIVE_UINT128_T 1 #define AVOID_NATIVE_UINT128_T 1
#include "flang/Common/uint128.h" #include "flang/Common/uint128.h"
#include "testing.h" #include "testing.h"
#include "llvm/Support/raw_ostream.h"
#include <cinttypes> #include <cinttypes>
#include <iostream>
#if (defined __GNUC__ || defined __clang__) && defined __SIZEOF_INT128__ #if (defined __GNUC__ || defined __clang__) && defined __SIZEOF_INT128__
#define HAS_NATIVE_UINT128_T 1 #define HAS_NATIVE_UINT128_T 1
@ -123,10 +123,10 @@ int main() {
} }
} }
#if HAS_NATIVE_UINT128_T #if HAS_NATIVE_UINT128_T
std::cout << "Environment has native __uint128_t\n"; llvm::outs() << "Environment has native __uint128_t\n";
TestVsNative(); TestVsNative();
#else #else
std::cout << "Environment lacks native __uint128_t\n"; llvm::outs() << "Environment lacks native __uint128_t\n";
#endif #endif
return testing::Complete(); return testing::Complete();
} }

View File

@ -21,6 +21,7 @@ add_executable(format-test
target_link_libraries(format-test target_link_libraries(format-test
FortranRuntime FortranRuntime
RuntimeTesting RuntimeTesting
LLVMSupport
) )
add_test(Format format-test) add_test(Format format-test)
@ -32,6 +33,7 @@ add_executable(hello-world
target_link_libraries(hello-world target_link_libraries(hello-world
FortranRuntime FortranRuntime
RuntimeTesting RuntimeTesting
LLVMSupport
) )
add_test(HelloWorld hello-world) add_test(HelloWorld hello-world)
@ -42,6 +44,7 @@ add_executable(external-hello-world
target_link_libraries(external-hello-world target_link_libraries(external-hello-world
FortranRuntime FortranRuntime
LLVMSupport
) )
add_executable(list-input-test add_executable(list-input-test
@ -51,6 +54,7 @@ add_executable(list-input-test
target_link_libraries(list-input-test target_link_libraries(list-input-test
FortranRuntime FortranRuntime
RuntimeTesting RuntimeTesting
LLVMSupport
) )
add_test(ListInput list-input-test) add_test(ListInput list-input-test)

View File

@ -3,9 +3,9 @@
#include "testing.h" #include "testing.h"
#include "../runtime/format-implementation.h" #include "../runtime/format-implementation.h"
#include "../runtime/io-error.h" #include "../runtime/io-error.h"
#include "llvm/Support/raw_ostream.h"
#include <cstdarg> #include <cstdarg>
#include <cstring> #include <cstring>
#include <iostream>
#include <string> #include <string>
#include <vector> #include <vector>
@ -93,13 +93,13 @@ void TestFormatContext::Check(Results &expect) {
if (expect != results) { if (expect != results) {
Fail() << "expected:"; Fail() << "expected:";
for (const std::string &s : expect) { for (const std::string &s : expect) {
std::cerr << ' ' << s; llvm::errs() << ' ' << s;
} }
std::cerr << "\ngot:"; llvm::errs() << "\ngot:";
for (const std::string &s : results) { for (const std::string &s : results) {
std::cerr << ' ' << s; llvm::errs() << ' ' << s;
} }
std::cerr << '\n'; llvm::errs() << '\n';
} }
expect.clear(); expect.clear();
results.clear(); results.clear();

View File

@ -3,8 +3,8 @@
#include "testing.h" #include "testing.h"
#include "../../runtime/descriptor.h" #include "../../runtime/descriptor.h"
#include "../../runtime/io-api.h" #include "../../runtime/io-api.h"
#include "llvm/Support/raw_ostream.h"
#include <cstring> #include <cstring>
#include <iostream>
using namespace Fortran::runtime; using namespace Fortran::runtime;
using namespace Fortran::runtime::io; using namespace Fortran::runtime::io;
@ -110,8 +110,8 @@ static void realInTest(
Fail() << '\'' << format << "' failed reading '" << data << "', status " Fail() << '\'' << format << "' failed reading '" << data << "', status "
<< static_cast<int>(status) << " iomsg '" << iomsg << "'\n"; << static_cast<int>(status) << " iomsg '" << iomsg << "'\n";
} else if (u.raw != want) { } else if (u.raw != want) {
Fail() << '\'' << format << "' failed reading '" << data << "', want 0x" Fail() << '\'' << format << "' failed reading '" << data << "', want 0x";
<< std::hex << want << ", got 0x" << u.raw << std::dec << '\n'; Fail().write_hex(want) << ", got 0x" << u.raw << '\n';
} }
} }

View File

@ -4,9 +4,9 @@
#include "../../runtime/descriptor.h" #include "../../runtime/descriptor.h"
#include "../../runtime/io-api.h" #include "../../runtime/io-api.h"
#include "../../runtime/io-error.h" #include "../../runtime/io-error.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm> #include <algorithm>
#include <cstring> #include <cstring>
#include <iostream>
using namespace Fortran::runtime; using namespace Fortran::runtime;
using namespace Fortran::runtime::io; using namespace Fortran::runtime::io;

View File

@ -1,10 +1,10 @@
#include "testing.h" #include "testing.h"
#include "../../runtime/terminator.h" #include "../../runtime/terminator.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm> #include <algorithm>
#include <cstdarg> #include <cstdarg>
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <iostream>
#include <string> #include <string>
static int failures{0}; static int failures{0};
@ -21,16 +21,16 @@ void StartTests() {
Fortran::runtime::Terminator::RegisterCrashHandler(CatchCrash); Fortran::runtime::Terminator::RegisterCrashHandler(CatchCrash);
} }
std::ostream &Fail() { llvm::raw_ostream &Fail() {
++failures; ++failures;
return std::cerr; return llvm::errs();
} }
int EndTests() { int EndTests() {
if (failures == 0) { if (failures == 0) {
std::cout << "PASS\n"; llvm::outs() << "PASS\n";
} else { } else {
std::cout << "FAIL " << failures << " tests\n"; llvm::outs() << "FAIL " << failures << " tests\n";
} }
return failures != 0; return failures != 0;
} }

View File

@ -4,8 +4,12 @@
#include <cstddef> #include <cstddef>
#include <iosfwd> #include <iosfwd>
namespace llvm {
class raw_ostream;
}
void StartTests(); void StartTests();
std::ostream &Fail(); llvm::raw_ostream &Fail();
int EndTests(); int EndTests();
void SetCharacter(char *, std::size_t, const char *); void SetCharacter(char *, std::size_t, const char *);