Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libexpr: diagnostics (warning/error), tags framework #9060

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ perl/Makefile.config
/src/libexpr/parser-tab.output
/src/libexpr/nix.tbl
/src/libexpr/tests/libnixexpr-tests
/src/libexpr/*.inc.hh

# /src/libstore/
*.gen.*
Expand Down
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
buildPackages.autoconf-archive
buildPackages.autoreconfHook
buildPackages.pkg-config
buildPackages.nixVersions.nix_2_14

# Tests
buildPackages.git
Expand Down
74 changes: 74 additions & 0 deletions src/libexpr/diagnostic.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/// diagnostic.hh - This file declares records that related to nix diagnostic
#pragma once

#include <string>
#include <vector>

#include "nixexpr.hh"

namespace nix {

/// The diagnostic
struct Diag
{
PosIdx begin;
/// Which diagnostic
enum ID {
#define NIX_DIAG_ID(ID) ID,
#include "diagnostics-id.inc.hh"
#undef NIX_DIAG_ID
};

/// Tags
enum Tag {
DT_None,
DT_Unnecessary,
DT_Deprecated,
};

Diag(PosIdx begin)
: begin(begin)
{
}

enum Severity { DL_Warning, DL_Error, DL_Note };

/// Additional information attached to some diagnostic.
/// elaborting the problem,
/// usaually points to a related piece of code.
std::vector<Diag> notes;

virtual Tag getTags() = 0;
virtual ID getID() = 0;
virtual Severity getServerity() = 0;
[[nodiscard]] virtual std::string format() const = 0;

virtual ~Diag() = default;
};

struct DiagnosticEngine
{
std::vector<std::unique_ptr<Diag>> Errors;
std::vector<std::unique_ptr<Diag>> Warnings;

void add(std::unique_ptr<Diag> D)
{
// Currently we just make the severity as-is
// we can use some flags to control (e.g. -Werror)
if (D->getServerity() == Diag::DL_Error) {
Errors.emplace_back(std::move(D));
} else {
Warnings.emplace_back(std::move(D));
}
}

void checkRaise(const PosTable & positions) const
{
if (!Errors.empty()) {
const Diag * back = Errors.back().get();
throw ParseError(ErrorInfo{.msg = back->format(), .errPos = positions[back->begin]});
}
}
};

} // namespace nix
54 changes: 54 additions & 0 deletions src/libexpr/diagnostics-gen.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Diagnostics.nix, declaratively record nix diagnostics
let
mkDeclaration =
{ name
, level
, body ? ""
, ctor ? "Diag${name}(PosIdx p): Diag(p) { }"
, format ? "return R\"(${message})\";"
, message
, tags ? "None"
}: ''
struct Diag${name} : Diag {
${body}
${ctor}
ID getID() override {
return DK_${name};
}
Tag getTags() override {
return DT_${tags};
}
Severity getServerity() override {
return DL_${level};
}
std::string format() const override {
${format}
}
};
'';
mkIDMacro =
{ name
, ...
}: ''
NIX_DIAG_ID(DK_${name})
'';

diagnostics = import ./diagnostics.nix;
in
{
declarations = ''
/// Generated from diagnostic-gen.nix
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- diagnostic-gen.nix
+ diagnostics-gen.nix

#pragma once
#include "diagnostic.hh"
namespace nix {
${builtins.concatStringsSep "" (map mkDeclaration diagnostics)}
} // namespace nix
'';
idmacros = ''
/// Generated from diagnostic-gen.nix
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

#ifdef NIX_DIAG_ID
${builtins.concatStringsSep "" (map mkIDMacro diagnostics)}
#endif
'';
}

71 changes: 71 additions & 0 deletions src/libexpr/diagnostics.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
let
kinds = {
warning = "Warning";
error = "Error";
};
tags = {
unnecessary = "Unnecessary";
deprecated = "Deprecated";
};
in
[
{
name = "PathHasTrailingSlash";
level = kinds.error;
message = "path has a trailing slash";
}
rec {
name = "InvalidInteger";
level = kinds.error;
message = "invalid integer '%1%'";
body = "std::string text;";
ctor = ''Diag${name}(PosIdx p, std::string text): Diag(p), text(std::move(text)) { }'';
format = "return hintfmt(\"${message}\", text).str();";
}
rec {
name = "InvalidFloat";
level = kinds.error;
message = "invalid float '%1%'";
body = "std::string text;";
ctor = ''Diag${name}(PosIdx p, std::string text): Diag(p), text(std::move(text)) { }'';
format = "return hintfmt(\"${message}\", text).str();";
}
{
name = "DynamicAttrsInLet";
level = kinds.error;
message = "dynamic attributes not allowed in let";
}
{
name = "URLLiteralsDisabled";
level = kinds.error;
message = "URL literals are disabled";
tags = tags.deprecated;
}
{
name = "URLLiterals";
level = kinds.warning;
message = "using deprecated URL literal syntax";
tags = tags.deprecated;
}
rec {
name = "HPathPure";
level = kinds.error;
message = "home-path '%s' can not be resolved in pure mode";
body = "std::string text;";
ctor = ''Diag${name}(PosIdx p, std::string text): Diag(p), text(std::move(text)) { }'';
format = "return hintfmt(\"${message}\", text).str();";
}
{
name = "InheritDynamic";
level = kinds.error;
message = "dynamic attributes not allowed in inherit";
}
rec {
name = "Syntax";
level = kinds.error;
message = "";
body = "std::string text;";
ctor = ''Diag${name}(PosIdx p, std::string text): Diag(p), text(std::move(text)) { }'';
format = "return text;";
}
]
93 changes: 93 additions & 0 deletions src/libexpr/lexer-prologue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#ifdef __clang__
#pragma clang diagnostic ignored "-Wunneeded-internal-declaration"
#endif

#include <boost/lexical_cast.hpp>

#include "nixexpr.hh"
#include "parser-tab.hh"

using namespace nix;

namespace nix {

static inline PosIdx makeCurPos(const YYLTYPE & loc, ParseData * data)
{
return data->state.positions.add(data->origin, loc.first_line, loc.first_column);
}

#define CUR_POS makeCurPos(*yylloc, data)

// backup to recover from yyless(0)
thread_local YYLTYPE prev_yylloc;

static void initLoc(YYLTYPE * loc)
{
loc->first_line = loc->last_line = 1;
loc->first_column = loc->last_column = 1;
}

static void adjustLoc(YYLTYPE * loc, const char * s, size_t len)
{
prev_yylloc = *loc;

loc->first_line = loc->last_line;
loc->first_column = loc->last_column;

for (size_t i = 0; i < len; i++) {
switch (*s++) {
case '\r':
if (*s == '\n') { /* cr/lf */
i++;
s++;
}
/* fall through */
case '\n':
++loc->last_line;
loc->last_column = 1;
break;
default:
++loc->last_column;
}
}
}

// we make use of the fact that the parser receives a private copy of the input
// string and can munge around in it.
static StringToken unescapeStr(SymbolTable & symbols, char * s, size_t length)
{
char * result = s;
char * t = s;
char c;
// the input string is terminated with *two* NULs, so we can safely take
// *one* character after the one being checked against.
while ((c = *s++)) {
if (c == '\\') {
c = *s++;
if (c == 'n')
*t = '\n';
else if (c == 'r')
*t = '\r';
else if (c == 't')
*t = '\t';
else
*t = c;
} else if (c == '\r') {
/* Normalise CR and CR/LF into LF. */
*t = '\n';
if (*s == '\n')
s++; /* cr/lf */
} else
*t = c;
t++;
}
return {result, size_t(t - result)};
}

}

#define YY_USER_INIT initLoc(yylloc)
#define YY_USER_ACTION adjustLoc(yylloc, yytext, yyleng);

#define PUSH_STATE(state) yy_push_state(state, yyscanner)
#define POP_STATE() yy_pop_state(yyscanner)
Loading
Loading