Skip to content

Commit

Permalink
Merge pull request #10 from MattX/error-directive
Browse files Browse the repository at this point in the history
Add #error preprocessor directive
  • Loading branch information
PhilippRados authored Dec 19, 2024
2 parents 261deb7 + b8a8968 commit 990e77c
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ cargo install wrecc
## Features
Since not all keywords are currently implemented wrecc uses [custom standard-headers](https://github.com/PhilippRados/wrecc/tree/master/include) which are built directly into the binary
### Preprocessor
The preprocessor implements all [C99 preprocessor directives](https://en.cppreference.com/w/c/keyword), except `#line`, `#error` and `#pragma`. Most prominently it currently also misses function-like macros which are on the agenda though.
The preprocessor implements all [C99 preprocessor directives](https://en.cppreference.com/w/c/keyword), except `#line` and `#pragma`. Most prominently it currently also misses function-like macros which are on the agenda though.

### Compiler
#### Supported Keywords <a name="keywords"></a>
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/error_pragma.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int main() {
return 42;
#error oh nooooo
}
2 changes: 1 addition & 1 deletion tests/snapshots/success_const_err
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ error: cannot assign variable which was declared 'const'
|
16 a.dig = Two;
| ^
error: cannot assign to 'struct Foo' that contains member 'dig' declared 'const'
error: cannot assign to 'struct Foo' that contains member 'dig' declared 'const'
| --> in tests/fixtures/const_err.c:17:5
|
17 a = b;
Expand Down
6 changes: 6 additions & 0 deletions tests/snapshots/success_error_pragma
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error: oh nooooo
| --> in tests/fixtures/error_pragma.c:3:2
|
3 #error oh nooooo
| ^
1 error generated.
8 changes: 5 additions & 3 deletions wrecc_compiler/src/compiler/common/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ pub enum ErrorKind {
ElifAfterElse,
TrailingTokens(&'static str),
MaxIncludeDepth(usize),
ErrorDirective(String),

Regular(&'static str), // generic error message when message only used once
Multiple(Vec<Error>),
Expand Down Expand Up @@ -226,7 +227,7 @@ impl ErrorKind {
}
ErrorKind::IncompleteFuncParam(func_name, qtype) => {
format!(
"function '{}' contains incomplete type '{}' as parameter",
"function '{}' contains incomplete type '{}' as parameter",
func_name, qtype
)
}
Expand All @@ -235,7 +236,7 @@ impl ErrorKind {
.to_string()
}
ErrorKind::TypeAlreadyExists(name, token) => {
format!("type '{}' already exists but not as {}", name, token)
format!("type '{}' already exists but not as {}", name, token)
}
ErrorKind::EnumForwardDecl => "cannot forward declare enums".to_string(),
ErrorKind::EmptyAggregate(token) => {
Expand Down Expand Up @@ -275,7 +276,7 @@ impl ErrorKind {
ErrorKind::ConstAssign => "cannot assign variable which was declared 'const'".to_string(),
ErrorKind::ConstStructAssign(ty, member) => {
format!(
"cannot assign to '{}' that contains member '{}' declared 'const'",
"cannot assign to '{}' that contains member '{}' declared 'const'",
ty, member
)
}
Expand Down Expand Up @@ -478,6 +479,7 @@ impl ErrorKind {
ErrorKind::MaxIncludeDepth(max) => {
format!("#include is nested too deeply, exceeds maximum-depth of {}", max)
}
ErrorKind::ErrorDirective(s) => s.clone(),

ErrorKind::Regular(s) => s.to_string(),
ErrorKind::Multiple(_) => {
Expand Down
1 change: 1 addition & 0 deletions wrecc_compiler/src/compiler/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ impl<'a> Scanner<'a> {
| PPKind::Else
| PPKind::Elif
| PPKind::Endif
| PPKind::Error
| PPKind::Undef
| PPKind::Define
| PPKind::Defined => {
Expand Down
12 changes: 12 additions & 0 deletions wrecc_compiler/src/preprocessor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,18 @@ impl<'a> Preprocessor<'a> {
}
self.conditional_block(directive)
}
TokenKind::Error => {
let _ = self.skip_whitespace();
let rest = self
.fold_until_token(TokenKind::Newline)
.into_iter()
.map(|t| t.kind.to_string())
.collect::<String>();
Err(Error::new(
&PPToken::from(&directive, self.filename),
ErrorKind::ErrorDirective(rest),
))
}
_ => Err(Error::new(
&PPToken::from(&directive, self.filename),
ErrorKind::InvalidDirective(directive.kind.to_string()),
Expand Down
8 changes: 6 additions & 2 deletions wrecc_compiler/src/preprocessor/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum TokenKind {
Else,
Endif,
Newline,
Error,
String(String),
CharLit(String),
Ident(String),
Expand Down Expand Up @@ -52,7 +53,7 @@ impl Token {
TokenKind::Hash | TokenKind::Other(_) => 1,
TokenKind::If => 2,
TokenKind::Else | TokenKind::Elif => 4,
TokenKind::Undef | TokenKind::Ifdef | TokenKind::Endif => 5,
TokenKind::Undef | TokenKind::Ifdef | TokenKind::Endif | TokenKind::Error => 5,
TokenKind::Define | TokenKind::Ifndef => 6,
TokenKind::Include | TokenKind::Defined => 7,
TokenKind::String(s)
Expand All @@ -77,7 +78,8 @@ impl TokenKind {
| TokenKind::If
| TokenKind::Elif
| TokenKind::Else
| TokenKind::Endif => Some(self.to_string()),
| TokenKind::Endif
| TokenKind::Error => Some(self.to_string()),
_ => None,
}
}
Expand All @@ -96,6 +98,7 @@ impl ToString for TokenKind {
TokenKind::Elif => "elif".to_string(),
TokenKind::Else => "else".to_string(),
TokenKind::Endif => "endif".to_string(),
TokenKind::Error => "error".to_string(),
TokenKind::Newline => "\n".to_string(),
TokenKind::String(s)
| TokenKind::CharLit(s)
Expand Down Expand Up @@ -140,6 +143,7 @@ impl Scanner {
("elif", TokenKind::Elif),
("else", TokenKind::Else),
("endif", TokenKind::Endif),
("error", TokenKind::Error),
]),
}
}
Expand Down

0 comments on commit 990e77c

Please sign in to comment.