Skip to content

Commit

Permalink
Support `undef
Browse files Browse the repository at this point in the history
  • Loading branch information
gjcoram committed Jul 11, 2024
1 parent eba9808 commit 04cb1fb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions openvaf/basedb/src/diagnostics/preprocessor_error.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ impl Diagnostic for PreprocessorDiagnostic {
message: "macro not found here".to_owned(),
}])
}
PreprocessorDiagnostic::MacroNotDefined { span, .. } => {
let span = span.to_file_span(&sm);

Report::warning().with_labels(vec![Label {
style: LabelStyle::Primary,
file_id: span.file,
range: span.range.into(),
message: "macro not defined here".to_owned(),
}])
}
PreprocessorDiagnostic::MacroRecursion { .. } => todo!(),
PreprocessorDiagnostic::FileNotFound { span, .. } => {
let labels = if let Some(span) = span {
Expand Down
2 changes: 2 additions & 0 deletions openvaf/preprocessor/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::sourcemap::CtxSpan;
pub enum PreprocessorDiagnostic {
MacroArgumentCountMismatch { expected: usize, found: usize, span: CtxSpan },
MacroNotFound { name: String, span: CtxSpan },
MacroNotDefined { name: String, span: CtxSpan },
MacroRecursion { name: String, span: CtxSpan },
FileNotFound { file: String, error: io::ErrorKind, span: Option<CtxSpan> },
InvalidTextFormat { span: Option<CtxSpan>, file: VfsPath, err: InvalidTextFormatErr },
Expand All @@ -23,6 +24,7 @@ impl_display! {
match PreprocessorDiagnostic{
MacroArgumentCountMismatch { expected, found, ..} => "argument mismatch expected {} but found {}!", expected, found;
MacroNotFound{name,..} => "macro '`{}' has not been declared", name;
MacroNotDefined{name,..} => "cannot undefine macro '`{}'", name;
MacroRecursion { name,..} => "macro '`{}' was called recursively",name;
FileNotFound { file, error, .. } => "failed to read '{}': {}", file, std::io::Error::from(*error);
InvalidTextFormat { file, ..} => "failed to read {}: file contents are not valid text", file;
Expand Down
2 changes: 2 additions & 0 deletions openvaf/preprocessor/src/parser.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ impl<'a, 'd> Parser<'a, 'd> {
"`else" => CompilerDirective::Else,
"`elsif" => CompilerDirective::ElseIf,
"`endif" => CompilerDirective::EndIf,
"`undef" => CompilerDirective::Undef,
_ => CompilerDirective::Macro,
}
}
Expand All @@ -330,5 +331,6 @@ pub enum CompilerDirective {
Else,
ElseIf,
EndIf,
Undef,
Macro,
}
13 changes: 13 additions & 0 deletions openvaf/preprocessor/src/processor.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,19 @@ impl<'a> Processor<'a> {
p.bump();
parse_condition(p, err, self, true);
}
CompilerDirective::Undef => {
p.bump();
let name = p.current_text();
if self.macros.contains_key(name) {
self.macros.remove(name);
} else {
err.push(PreprocessorDiagnostic::MacroNotDefined {
name: name.to_owned(),
span: p.current_span()
})
}
p.bump();
}
CompilerDirective::Macro => {
let (call, range) =
parse_macro_call(p, err, &[], &mut self.source_map, p.end());
Expand Down

0 comments on commit 04cb1fb

Please sign in to comment.