-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libnixf: introduce
nixf-tidy
, perform analysis, lints and emit json (…
…#446) Let's bring nix world automatic linting in the CI.
- Loading branch information
Showing
6 changed files
with
158 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/// \file | ||
/// \brief Provide jsonified diagnostic, for other languages/structured output. | ||
|
||
#include "Diagnostic.h" | ||
#include "nixf/Basic/Range.h" | ||
|
||
#include <nlohmann/json.hpp> | ||
|
||
namespace nixf { | ||
|
||
void to_json // NOLINT(readability-identifier-naming) | ||
(nlohmann::json &R, const LexerCursor &LC); | ||
|
||
void to_json // NOLINT(readability-identifier-naming) | ||
(nlohmann::json &R, const LexerCursorRange &LCR); | ||
|
||
void to_json // NOLINT(readability-identifier-naming) | ||
(nlohmann::json &R, const PartialDiagnostic &D); | ||
|
||
void to_json // NOLINT(readability-identifier-naming) | ||
(nlohmann::json &R, const Diagnostic &D); | ||
|
||
void to_json // NOLINT(readability-identifier-naming) | ||
(nlohmann::json &R, const Note &N); | ||
|
||
void to_json // NOLINT(readability-identifier-naming) | ||
(nlohmann::json &R, const TextEdit &D); | ||
|
||
void to_json // NOLINT(readability-identifier-naming) | ||
(nlohmann::json &R, const Fix &F); | ||
|
||
} // namespace nixf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include "nixf/Basic/JSONDiagnostic.h" | ||
#include "nixf/Basic/Diagnostic.h" | ||
#include "nixf/Basic/Range.h" | ||
|
||
using namespace nixf; | ||
using nlohmann::json; | ||
|
||
void nixf::to_json(json &R, const LexerCursor &LC) { | ||
R = json{ | ||
{"line", LC.line()}, | ||
{"column", LC.column()}, | ||
{"offset", LC.offset()}, | ||
}; | ||
} | ||
|
||
void nixf::to_json(json &R, const LexerCursorRange &LCR) { | ||
R = json{ | ||
{"lCur", LCR.lCur()}, | ||
{"rCur", LCR.rCur()}, | ||
}; | ||
} | ||
|
||
void nixf::to_json(json &R, const PartialDiagnostic &PD) { | ||
R = json{ | ||
{"args", PD.args()}, | ||
{"tag", PD.tags()}, | ||
{"range", PD.range()}, | ||
}; | ||
} | ||
|
||
void nixf::to_json(json &R, const Diagnostic &D) { | ||
to_json(R, static_cast<const PartialDiagnostic &>(D)); | ||
R["kind"] = D.kind(); | ||
R["severity"] = Diagnostic::severity(D.kind()); | ||
R["message"] = D.message(); | ||
R["sname"] = D.sname(); | ||
R["notes"] = D.notes(); | ||
R["fixes"] = D.fixes(); | ||
} | ||
|
||
void nixf::to_json(json &R, const Note &N) { | ||
to_json(R, static_cast<const PartialDiagnostic &>(N)); | ||
R["kind"] = N.kind(); | ||
R["sname"] = N.sname(); | ||
R["message"] = N.message(); | ||
} | ||
|
||
void nixf::to_json(json &R, const Fix &F) { | ||
R = json{ | ||
{"edits", F.edits()}, | ||
{"message", F.message()}, | ||
}; | ||
} | ||
|
||
void nixf::to_json(json &R, const TextEdit &D) { | ||
R = json{ | ||
{"range", D.oldRange()}, | ||
{"newText", D.newText()}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/// \file | ||
/// \brief nixf-tidy, provide linting based on libnixf. | ||
|
||
#include "nixf/Basic/JSONDiagnostic.h" | ||
#include "nixf/Basic/Nodes/Basic.h" | ||
#include "nixf/Parse/Parser.h" | ||
#include "nixf/Sema/VariableLookup.h" | ||
|
||
#include <iostream> | ||
#include <sstream> | ||
|
||
using namespace nixf; | ||
|
||
namespace { | ||
|
||
/// Pretty print the output. | ||
bool PrettyPrint = false; | ||
|
||
/// Enable variable lookup warnings. | ||
bool VLA; | ||
|
||
void parseArgs(int Argc, const char *Argv[]) { | ||
for (int I = 0; I < Argc; I++) { | ||
if (std::string_view(Argv[I]) == "--pretty-print") | ||
PrettyPrint = true; | ||
else if (std::string_view(Argv[I]) == "--variable-lookup") | ||
VLA = true; | ||
} | ||
} | ||
|
||
} // namespace | ||
|
||
int main(int Argc, const char *Argv[]) { | ||
parseArgs(Argc, Argv); | ||
std::ostringstream Inputs; | ||
Inputs << std::cin.rdbuf(); | ||
std::string Src = Inputs.str(); | ||
|
||
std::vector<nixf::Diagnostic> Diags; | ||
std::shared_ptr<nixf::Node> AST = nixf::parse(Src, Diags); | ||
|
||
if (VLA) { | ||
VariableLookupAnalysis V(Diags); | ||
if (AST) | ||
V.runOnAST(*AST); | ||
} | ||
|
||
nlohmann::json V; | ||
to_json(V, Diags); | ||
|
||
if (PrettyPrint) | ||
std::cout << std::setw(4); | ||
std::cout << V << "\n"; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters