Skip to content

Commit

Permalink
added --write-error-line
Browse files Browse the repository at this point in the history
  • Loading branch information
oblaser committed Apr 28, 2021
1 parent 1dd65a7 commit d80edad
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 22 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ potoroo -if FILE (-od DIR | -of FILE) [options]
| `-t TAG` | Specify the tag |
| `-Werror` | Handles warnings as errors (only in processor, the jobfile parser is unaffected by this option). Results in not writing the output file if any warning occured. |
| `-Wsup LIST` | Suppresses the reporting of the specified warnings. LIST is a comma separated (no spaces) list of integer warning IDs. (Only in processor, the jobfile parser is unaffected by this option. May be useful in combination with `-Werror`) |
| `--write-error-line TEXT` | Instead of deleting the output file on error, writes _TEXT_ to it |
| `--copy` | Copy, replaces the existing file only if it is older than the input file |
| `--copy-ow` | Copy, overwrites the existing file |

Expand Down
3 changes: 2 additions & 1 deletion src/application/arg.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
\author Oliver Blaser
\date 06.04.2021
\date 28.04.2021
\copyright GNU GPLv3 - Copyright (c) 2021 Oliver Blaser
*/
Expand Down Expand Up @@ -105,6 +105,7 @@ potoroo::Arg::Arg(const std::string& arg)
else if (arg == argStr_wSup) type = ArgType::wSup;
else if (arg == argStr_copy) type = ArgType::copy;
else if (arg == argStr_copyow) type = ArgType::copyow;
else if (arg == argStr_wrErrLn) type = ArgType::wrErrLn;
else if ((arg == argStr_help) || (arg == argStr_help_alt)) type = ArgType::help;
else if ((arg == argStr_version) || (arg == argStr_version_alt)) type = ArgType::version;
else type = ArgType::argType_invalid;
Expand Down
4 changes: 3 additions & 1 deletion src/application/arg.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
\author Oliver Blaser
\date 06.04.2021
\date 28.04.2021
\copyright GNU GPLv3 - Copyright (c) 2021 Oliver Blaser
*/
Expand All @@ -24,6 +24,7 @@ namespace potoroo
const std::string argStr_forceJf = "--force-jf";
const std::string argStr_wError = "-Werror";
const std::string argStr_wSup = "-Wsup";
const std::string argStr_wrErrLn = "--write-error-line";
const std::string argStr_copy = "--copy";
const std::string argStr_copyow = "--copy-ow";
const std::string argStr_help = "-h";
Expand All @@ -45,6 +46,7 @@ namespace potoroo
forceJf,
wError,
wSup,
wrErrLn,
copy,
copyow
};
Expand Down
31 changes: 23 additions & 8 deletions src/application/job.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
\author Oliver Blaser
\date 07.04.2021
\date 28.04.2021
\copyright GNU GPLv3 - Copyright (c) 2021 Oliver Blaser
*/
Expand Down Expand Up @@ -267,13 +267,17 @@ namespace


potoroo::Job::Job()
: wError(false), validity(false), errorMsg("unset"), mode(JobMode::proc)
: wError(false), wrErrLn(false), wrErrLnStr("--write-error-line"), validity(false), errorMsg("unset"), mode(JobMode::proc)
{
setWSupList(nullptr, 0);
}

potoroo::Job::Job(const std::string& inputFile, const std::string& outputFile, const std::string& tag, bool warningAsError, JobMode jobMode, const std::string* wSup)
: tag(tag), wError(warningAsError), validity(true), mode(jobMode)
potoroo::Job::Job(const std::string& inputFile, const std::string& outputFile, const std::string& tag,
bool warningAsError,
bool writeErrorLine, const std::string& writeErrorLineStr,
JobMode jobMode,
const std::string* wSup)
: tag(tag), wError(warningAsError), wrErrLn(writeErrorLine), wrErrLnStr(writeErrorLineStr), validity(true), mode(jobMode)
{
try { inFile = fs::path(inputFile).lexically_normal().string(); }
catch (...) { inFile = string(inputFile); }
Expand Down Expand Up @@ -331,6 +335,16 @@ bool potoroo::Job::warningAsError() const
return wError;
}

bool potoroo::Job::writeErrorLine() const
{
return wrErrLn;
}

std::string potoroo::Job::writeErrorLineStr() const
{
return wrErrLnStr;
}

const std::vector<int>& potoroo::Job::getWSupList() const
{
return wSupList;
Expand Down Expand Up @@ -457,7 +471,8 @@ std::ostream& potoroo::operator<<(std::ostream& os, const Job& j)
else os << " #invalid job mode#";

if (j.warningAsError()) os << " Werror";
if (j.getWSupList().size() > 0) os << " Wsup "+j.wSupListToString();
if (j.getWSupList().size() > 0) os << " Wsup " + j.wSupListToString();
if (j.writeErrorLine()) os << " " << argStr_wrErrLn;

return os;
}
Expand Down Expand Up @@ -534,10 +549,10 @@ Result potoroo::Job::parseFile(const std::string& filename, std::vector<Job>& jo
jobs.push_back(job);
}
#endif
}
}

return r;
}
}

Job potoroo::Job::parseArgs(const ArgList& args)
{
Expand Down Expand Up @@ -605,7 +620,7 @@ Job potoroo::Job::parseArgs(const ArgList& args)
string* wSupList = nullptr;
if (args.get(ArgType::wSup).isValid()) wSupList = &tmpWSupList;

try { return Job(inPath.string(), out, tag, args.contains(ArgType::wError), mode, wSupList); }
try { return Job(inPath.string(), out, tag, args.contains(ArgType::wError), args.contains(ArgType::wrErrLn), args.get(ArgType::wrErrLn).getValue(), mode, wSupList); }
catch (exception& ex) { return invalidInFilenameJob(in, ex.what()); }
catch (...) { return invalidInFilenameJob(in, ""); }
}
12 changes: 10 additions & 2 deletions src/application/job.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
\author Oliver Blaser
\date 06.04.2021
\date 28.04.2021
\copyright GNU GPLv3 - Copyright (c) 2021 Oliver Blaser
*/
Expand Down Expand Up @@ -37,7 +37,11 @@ namespace potoroo
{
public:
Job();
Job(const std::string& inputFile, const std::string& outputFile, const std::string& tag, bool warningAsError = false, JobMode mode = JobMode::proc, const std::string* wSup = nullptr);
Job(const std::string& inputFile, const std::string& outputFile, const std::string& tag,
bool warningAsError = false,
bool writeErrorLine = false, const std::string& writeErrorLineStr = std::string("--write-error-line"),
JobMode mode = JobMode::proc,
const std::string* wSup = nullptr);

void setValidity(bool validity);
void setErrorMsg(const std::string& msg);
Expand All @@ -47,6 +51,8 @@ namespace potoroo
std::string getTag() const;
JobMode getMode() const;
bool warningAsError() const;
bool writeErrorLine() const;
std::string writeErrorLineStr() const;
const std::vector<int>& getWSupList() const;

void setInputFile(const std::string& inputFile);
Expand Down Expand Up @@ -76,6 +82,8 @@ namespace potoroo
std::string tag;
JobMode mode;
bool wError;
bool wrErrLn;
std::string wrErrLnStr;
std::vector<int> wSupList;

bool validity = false;
Expand Down
32 changes: 25 additions & 7 deletions src/application/processor.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
\author Oliver Blaser
\date 08.04.2021
\date 28.04.2021
\copyright GNU GPLv3 - Copyright (c) 2021 Oliver Blaser
*/
Expand All @@ -13,6 +13,7 @@
#include <string>
#include <vector>

#include "arg.h"
#include "processor.h"
#include "middleware/cliTextFormat.h"
#include "middleware/util.h"
Expand Down Expand Up @@ -46,7 +47,7 @@ namespace

wID_rmOut_file,
wID_rmOut_dir,
wID_include_emptyFile, // r += warn(ewiFile, wID_, job, , ProcPos());
wID_include_emptyFile,
wID_include_multiInc,
wID_include_loop,
wID_tagInRMx,
Expand Down Expand Up @@ -942,10 +943,6 @@ Result potoroo::processJob(const Job& job, bool forceOutfLineEndLF) noexcept
{
++r.err;
printError(ewiFile, ex.what());

#if PRJ_DEBUG
int ___dbg_breakpoint = 0;
#endif
}
catch (...)
{
Expand All @@ -960,7 +957,28 @@ Result potoroo::processJob(const Job& job, bool forceOutfLineEndLF) noexcept
printError(ewiFile, "###[@Werror@] " + to_string(r.warn) + " warnings");
}

if ((r.err > 0) && !fs::equivalent(inf, outf)) r += rmOut(outf, ewiFile, job, createdOutDir);
if (r.err > 0)
{
if (job.writeErrorLine())
{
const string exMsg = "###[@" + argStr_wrErrLn + "@] could not write file";

try
{
ofstream ofs;
ofs.exceptions(ios::failbit | ios::badbit | ios::eofbit);
ofs.open(outf, ios::out | ios::binary);
string str = job.writeErrorLineStr();
ofs.write(str.c_str(), str.length());
}
catch (...)
{
++r.err;
printError(ewiFile, exMsg);
}
}
else if(!fs::equivalent(inf, outf)) r += rmOut(outf, ewiFile, job, createdOutDir);
}

return r;
}
Expand Down
8 changes: 6 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
\author Oliver Blaser
\date 07.04.2021
\date 28.04.2021
\copyright GNU GPLv3 - Copyright (c) 2021 Oliver Blaser
*/
Expand Down Expand Up @@ -60,8 +60,9 @@ namespace
cout << left << setw(lw) << " " + argStr_od + " DIR" << "output directory (same filename)" << endl;
cout << left << setw(lw) << " " + argStr_tag + " TAG" << "specify the tag" << endl;
cout << left << setw(lw) << " " + argStr_wError << "handles warnings as errors (in processor, the jobfile parser is unaffected)" << endl;
cout << left << setw(lw) << " " + argStr_wSup+" LIST" << "suppresses the reporting of the specified warnings. LIST is a comma separated" << endl;
cout << left << setw(lw) << " " + argStr_wSup + " LIST" << "suppresses the reporting of the specified warnings. LIST is a comma separated" << endl;
cout << left << setw(lw) << " " << "list of integer warning IDs. (in processor, the jobfile parser is unaffected)" << endl;
cout << left << setw(lw) << " " + argStr_wrErrLn + " TEXT" << " instead of deleting the output file on error, writes TEXT to it" << endl;
cout << left << setw(lw) << " " + argStr_copy << "copy, replaces the existing file only if it is older than the input file" << endl;
cout << left << setw(lw) << " " + argStr_copyow << "copy, overwrites the existing file" << endl;
cout << endl;
Expand All @@ -75,6 +76,9 @@ namespace
cout << left << setw(lwTag) << " batch" << left << setw(lwTagStr) << tagBatch << "for batch scripts" << endl;
cout << left << setw(lwTag) << " custom:CT" << left << setw(lwTagStr) << "CT" << "to use a custom comment tag" << endl;
cout << endl;
cout << endl;
cout << "Website:" << endl << " <https://github.com/oblaser/potoroo>" << endl;
cout << endl;
}

void printVersion()
Expand Down
12 changes: 11 additions & 1 deletion test/system/processor/potorooJobs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# author Oliver Blaser
# date 08.04.2021
# date 28.04.2021
# copyright GNU GPLv3 - Copyright (c) 2021 Oliver Blaser
#

Expand Down Expand Up @@ -31,6 +31,14 @@
-if bash.sh -of 000_deploy/bash-customTag.sh -t custom:#*#p


#
# test --write-error-line option
#

-if js/index.js -of 000_deploy/jsERRLN/1.js -Werror -Wsup 107,106 --write-error-line "// potoroo threw an error in this file"
-if js/index.js -of 000_deploy/jsERRLN/2.js -Werror -Wsup 107,106


#
# test behaviour on errors
#
Expand All @@ -40,6 +48,8 @@
#-if ./utf32BE.php -od ./000_deploy
#-if ./utf32LE.php -od ./000_deploy

#-if ./unknownExt.txt -od ./000_deploy

# to test if the file does not get overwritten:
-if index.html -od . -t cpp

Expand Down

0 comments on commit d80edad

Please sign in to comment.