From dd7b8183a5065bc0279d9bd87c27dd0ac91ccd8a Mon Sep 17 00:00:00 2001 From: Ben Burdette Date: Wed, 1 Apr 2020 16:20:20 -0600 Subject: [PATCH] indenting --- src/libutil/error.cc | 256 +++++++++++++++++++++---------------------- src/libutil/error.hh | 244 ++++++++++++++++++++--------------------- 2 files changed, 250 insertions(+), 250 deletions(-) diff --git a/src/libutil/error.cc b/src/libutil/error.cc index 2b7a079dc..4480e80bf 100644 --- a/src/libutil/error.cc +++ b/src/libutil/error.cc @@ -10,155 +10,155 @@ optional ErrorInfo::programName = std::nullopt; // return basic_format? string showErrLine(ErrLine &errLine) { - if (errLine.columnRange.has_value()) - { - return (format("(%1%:%2%)") % errLine.lineNumber % errLine.columnRange->start).str(); - } - else - { - return (format("(%1%)") % errLine.lineNumber).str(); - }; + if (errLine.columnRange.has_value()) + { + return (format("(%1%:%2%)") % errLine.lineNumber % errLine.columnRange->start).str(); + } + else + { + return (format("(%1%)") % errLine.lineNumber).str(); + }; } void printCodeLines(string &prefix, NixCode &nixCode) { - + if (nixCode.errLine.has_value()) - { - // previous line of code. - if (nixCode.errLine->prevLineOfCode.has_value()) { - cout << format("%1% %|2$5d|| %3%") - % prefix - % (nixCode.errLine->lineNumber - 1) - % *nixCode.errLine->prevLineOfCode - << endl; - } - - // line of code containing the error.%2$+5d% - cout << format("%1% %|2$5d|| %3%") - % prefix - % (nixCode.errLine->lineNumber) - % nixCode.errLine->errLineOfCode - << endl; - - // error arrows for the column range. - if (nixCode.errLine->columnRange.has_value()) { - int start = nixCode.errLine->columnRange->start; - std::string spaces; - for (int i = 0; i < start; ++i) - { - spaces.append(" "); - } + // previous line of code. + if (nixCode.errLine->prevLineOfCode.has_value()) { + cout << format("%1% %|2$5d|| %3%") + % prefix + % (nixCode.errLine->lineNumber - 1) + % *nixCode.errLine->prevLineOfCode + << endl; + } + + // line of code containing the error.%2$+5d% + cout << format("%1% %|2$5d|| %3%") + % prefix + % (nixCode.errLine->lineNumber) + % nixCode.errLine->errLineOfCode + << endl; + + // error arrows for the column range. + if (nixCode.errLine->columnRange.has_value()) + { + int start = nixCode.errLine->columnRange->start; + std::string spaces; + for (int i = 0; i < start; ++i) + { + spaces.append(" "); + } + + int len = nixCode.errLine->columnRange->len; + std::string arrows; + for (int i = 0; i < len; ++i) + { + arrows.append("^"); + } + + cout << format("%1% |%2%" ANSI_RED "%3%" ANSI_NORMAL) % prefix % spaces % arrows << endl; + } + + + + // next line of code. + if (nixCode.errLine->nextLineOfCode.has_value()) { + cout << format("%1% %|2$5d|| %3%") + % prefix + % (nixCode.errLine->lineNumber + 1) + % *nixCode.errLine->nextLineOfCode + << endl; + } - int len = nixCode.errLine->columnRange->len; - std::string arrows; - for (int i = 0; i < len; ++i) - { - arrows.append("^"); - } - - cout << format("%1% |%2%" ANSI_RED "%3%" ANSI_NORMAL) % prefix % spaces % arrows << endl; } - - - // next line of code. - if (nixCode.errLine->nextLineOfCode.has_value()) { - cout << format("%1% %|2$5d|| %3%") - % prefix - % (nixCode.errLine->lineNumber + 1) - % *nixCode.errLine->nextLineOfCode - << endl; - } - - } - } void printErrorInfo(ErrorInfo &einfo) { - int errwidth = 80; - string prefix = " "; + int errwidth = 80; + string prefix = " "; - string levelString; - switch (einfo.level) - { - case ErrLevel::elError: - { - levelString = ANSI_RED; - levelString += "error:"; - levelString += ANSI_NORMAL; - break; - } - case ErrLevel::elWarning: - { - levelString = ANSI_YELLOW; - levelString += "warning:"; - levelString += ANSI_NORMAL; - break; - } - default: - { - levelString = (format("invalid error level: %1%") % einfo.level).str(); - break; - } - } - - int ndl = prefix.length() + levelString.length() + 3 + einfo.name.length() + einfo.programName.value_or("").length(); - int dashwidth = ndl > (errwidth - 3) ? 3 : errwidth - ndl; - - string dashes; - for (int i = 0; i < dashwidth; ++i) - dashes.append("-"); - - // divider. - cout << format("%1%%2%" ANSI_BLUE " %3% %4% %5% %6%" ANSI_NORMAL) - % prefix - % levelString - % "---" - % einfo.name - % dashes - % einfo.programName.value_or("") - << endl; - - // filename. - if (einfo.nixCode.has_value()) - { - if (einfo.nixCode->nixFile.has_value()) + string levelString; + switch (einfo.level) { - string eline = einfo.nixCode->errLine.has_value() - ? string(" ") + showErrLine(*einfo.nixCode->errLine) - : ""; - - cout << format("%1%in file: " ANSI_BLUE "%2%%3%" ANSI_NORMAL) - % prefix % *einfo.nixCode->nixFile % eline << endl; - cout << prefix << endl; + case ErrLevel::elError: + { + levelString = ANSI_RED; + levelString += "error:"; + levelString += ANSI_NORMAL; + break; + } + case ErrLevel::elWarning: + { + levelString = ANSI_YELLOW; + levelString += "warning:"; + levelString += ANSI_NORMAL; + break; + } + default: + { + levelString = (format("invalid error level: %1%") % einfo.level).str(); + break; + } } - else + + int ndl = prefix.length() + levelString.length() + 3 + einfo.name.length() + einfo.programName.value_or("").length(); + int dashwidth = ndl > (errwidth - 3) ? 3 : errwidth - ndl; + + string dashes; + for (int i = 0; i < dashwidth; ++i) + dashes.append("-"); + + // divider. + cout << format("%1%%2%" ANSI_BLUE " %3% %4% %5% %6%" ANSI_NORMAL) + % prefix + % levelString + % "---" + % einfo.name + % dashes + % einfo.programName.value_or("") + << endl; + + // filename. + if (einfo.nixCode.has_value()) { - cout << format("%1%from command line argument") % prefix << endl; - cout << prefix << endl; + if (einfo.nixCode->nixFile.has_value()) + { + string eline = einfo.nixCode->errLine.has_value() + ? string(" ") + showErrLine(*einfo.nixCode->errLine) + : ""; + + cout << format("%1%in file: " ANSI_BLUE "%2%%3%" ANSI_NORMAL) + % prefix % *einfo.nixCode->nixFile % eline << endl; + cout << prefix << endl; + } + else + { + cout << format("%1%from command line argument") % prefix << endl; + cout << prefix << endl; + } } - } - // description - cout << prefix << einfo.description << endl; - cout << prefix << endl; - - // lines of code. - if (einfo.nixCode.has_value()) - { - printCodeLines(prefix, *einfo.nixCode); + // description + cout << prefix << einfo.description << endl; cout << prefix << endl; - } - // hint - if (einfo.hint.has_value()) - { - cout << prefix << *einfo.hint << endl; - cout << prefix << endl; - } + // lines of code. + if (einfo.nixCode.has_value()) + { + printCodeLines(prefix, *einfo.nixCode); + cout << prefix << endl; + } + + // hint + if (einfo.hint.has_value()) + { + cout << prefix << *einfo.hint << endl; + cout << prefix << endl; + } } } diff --git a/src/libutil/error.hh b/src/libutil/error.hh index 614f08a35..00c32ff21 100644 --- a/src/libutil/error.hh +++ b/src/libutil/error.hh @@ -11,45 +11,45 @@ namespace nix { -typedef enum - { elWarning - , elError - } ErrLevel; +typedef enum { + elWarning + , elError + } ErrLevel; class ColumnRange { - public: - unsigned int start; - unsigned int len; + public: + unsigned int start; + unsigned int len; }; class ErrorInfo; class ErrLine { - public: - int lineNumber; - optional columnRange; - optional prevLineOfCode; - string errLineOfCode; - optional nextLineOfCode; + public: + int lineNumber; + optional columnRange; + optional prevLineOfCode; + string errLineOfCode; + optional nextLineOfCode; }; class NixCode { - public: - optional nixFile; - optional errLine; + public: + optional nixFile; + optional errLine; - ErrLine& ensureErrLine() - { - if (!this->errLine.has_value()) - this->errLine = optional(ErrLine()); - return *this->errLine; - } + ErrLine& ensureErrLine() + { + if (!this->errLine.has_value()) + this->errLine = optional(ErrLine()); + return *this->errLine; + } }; // ------------------------------------------------- // ErrorInfo. -// Forward friend class declarations. "builder classes" +// Forward friend class declarations. "builder classes" template class AddName; @@ -76,170 +76,170 @@ class AddLOC; // The error info class itself. class ErrorInfo { - public: - ErrLevel level; - string name; - string description; - optional nixCode; - optional hint; - ErrorInfo& GetEI() { return *this; } + public: + ErrLevel level; + string name; + string description; + optional nixCode; + optional hint; + ErrorInfo& GetEI() { return *this; } - static optional programName; + static optional programName; - // give these access to the private constructor, - // when they are direct descendants (children but not grandchildren). - friend AddName; - friend AddDescription; - friend AddNixCode; - friend AddNixFile; - friend AddErrLine; - friend AddLineNumber; - friend AddColumnRange; - friend AddLOC; - - NixCode& ensureNixCode() - { - if (!this->nixCode.has_value()) - this->nixCode = optional(NixCode()); - return *this->nixCode; - } - protected: - // constructor is protected, so only the builder classes can create an ErrorInfo. - ErrorInfo(ErrLevel level) { this->level = level; } + // give these access to the private constructor, + // when they are direct descendants (children but not grandchildren). + friend AddName; + friend AddDescription; + friend AddNixCode; + friend AddNixFile; + friend AddErrLine; + friend AddLineNumber; + friend AddColumnRange; + friend AddLOC; + + NixCode& ensureNixCode() + { + if (!this->nixCode.has_value()) + this->nixCode = optional(NixCode()); + return *this->nixCode; + } + protected: + // constructor is protected, so only the builder classes can create an ErrorInfo. + ErrorInfo(ErrLevel level) { this->level = level; } }; // Init as error class EIError : public ErrorInfo { - protected: - EIError() : ErrorInfo(elError) {} + protected: + EIError() : ErrorInfo(elError) {} }; // Init as warning class EIWarning : public ErrorInfo { - protected: - EIWarning() : ErrorInfo(elWarning) {} + protected: + EIWarning() : ErrorInfo(elWarning) {} }; // Builder class definitions. template class AddName : private T { - public: - T& name(const std::string &name){ - GetEI().name = name; - return *this; - } - protected: - ErrorInfo& GetEI() { return T::GetEI(); } + public: + T& name(const std::string &name){ + GetEI().name = name; + return *this; + } + protected: + ErrorInfo& GetEI() { return T::GetEI(); } }; template class AddDescription : private T { - public: - T& description(const std::string &description){ - GetEI().description = description; - return *this; - } - protected: - ErrorInfo& GetEI() { return T::GetEI(); } + public: + T& description(const std::string &description){ + GetEI().description = description; + return *this; + } + protected: + ErrorInfo& GetEI() { return T::GetEI(); } }; template class AddNixFile : private T { - public: - T& nixFile(string filename) { - GetEI().ensureNixCode().nixFile = filename; - return *this; - } - protected: - ErrorInfo& GetEI() { return T::GetEI(); } + public: + T& nixFile(string filename) { + GetEI().ensureNixCode().nixFile = filename; + return *this; + } + protected: + ErrorInfo& GetEI() { return T::GetEI(); } }; template class AddLineNumber : private T { - public: - T& lineNumber(int lineNumber) { - GetEI().ensureNixCode().ensureErrLine().lineNumber = lineNumber; - return *this; - } - protected: - ErrorInfo& GetEI() { return T::GetEI(); } + public: + T& lineNumber(int lineNumber) { + GetEI().ensureNixCode().ensureErrLine().lineNumber = lineNumber; + return *this; + } + protected: + ErrorInfo& GetEI() { return T::GetEI(); } }; template class AddColumnRange : private T { - public: - T& columnRange(unsigned int start, unsigned int len) { - GetEI().ensureNixCode().ensureErrLine().columnRange = { start, len }; - return *this; - } - protected: - ErrorInfo& GetEI() { return T::GetEI(); } + public: + T& columnRange(unsigned int start, unsigned int len) { + GetEI().ensureNixCode().ensureErrLine().columnRange = { start, len }; + return *this; + } + protected: + ErrorInfo& GetEI() { return T::GetEI(); } }; template class AddLOC : private T { - public: - T& linesOfCode(optional prevloc, string loc, optional nextloc) { - GetEI().ensureNixCode().ensureErrLine().prevLineOfCode = prevloc; - GetEI().ensureNixCode().ensureErrLine().errLineOfCode = loc; - GetEI().ensureNixCode().ensureErrLine().nextLineOfCode = nextloc; - return *this; - } - protected: - ErrorInfo& GetEI() { return T::GetEI(); } + public: + T& linesOfCode(optional prevloc, string loc, optional nextloc) { + GetEI().ensureNixCode().ensureErrLine().prevLineOfCode = prevloc; + GetEI().ensureNixCode().ensureErrLine().errLineOfCode = loc; + GetEI().ensureNixCode().ensureErrLine().nextLineOfCode = nextloc; + return *this; + } + protected: + ErrorInfo& GetEI() { return T::GetEI(); } }; template class yellowify { public: - yellowify(T &s) : value(s) {} - T &value; + yellowify(T &s) : value(s) {} + T &value; }; template std::ostream& operator<<(std::ostream &out, const yellowify &y) { - return out << ANSI_YELLOW << y.value << ANSI_NORMAL; + return out << ANSI_YELLOW << y.value << ANSI_NORMAL; } // hint format shows templated values in yellow. class hintfmt { - public: - hintfmt(string format) :fmt(format) {} - template - hintfmt& operator%(const T &value) { fmt % yellowify(value); return *this; } + public: + hintfmt(string format) :fmt(format) {} + template + hintfmt& operator%(const T &value) { fmt % yellowify(value); return *this; } + + template + friend class AddHint; + private: + format fmt; - template - friend class AddHint; - private: - format fmt; - }; template class AddHint : private T { - public: - T& hint(hintfmt &hfmt) { - GetEI().hint = optional(hfmt.fmt.str()); - return *this; - } - T& nohint() { - GetEI().hint = std::nullopt; - return *this; - } - protected: - ErrorInfo& GetEI() { return T::GetEI(); } + public: + T& hint(hintfmt &hfmt) { + GetEI().hint = optional(hfmt.fmt.str()); + return *this; + } + T& nohint() { + GetEI().hint = std::nullopt; + return *this; + } + protected: + ErrorInfo& GetEI() { return T::GetEI(); } }; // --------------------------------------------------------