Skip to content

Commit

Permalink
fix: macro propagation bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Sarrus1 committed Mar 8, 2024
1 parent 1619ae6 commit ea347ad
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 11 deletions.
7 changes: 2 additions & 5 deletions crates/preprocessor/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,8 @@ pub(crate) fn preprocess_file_query(
HashableHashMap::default(),
HashableHashSet::default(),
);
let Some(params) = res.get(&root_file_id) else {
log::warn!(
"No preprocessing params found for file_id: {}",
root_file_id
);
let Some(params) = res.get(&file_id) else {
log::warn!("No preprocessing params found for file_id: {}", file_id);
return Arc::new(PreprocessingResult::default(db.file_text(file_id).as_ref()));
};

Expand Down
3 changes: 3 additions & 0 deletions crates/preprocessor/src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ impl<'a> IfCondition<'a> {
}
_ => {
if looking_for_defined {
log::debug!("Looking for defined macro: {:?}", symbol.text());
log::debug!("Defined macros: {:?}", self.macro_store);
if let Some(macro_) = self.macro_store.get(&symbol.text()) {
log::debug!("Found defined macro: {:?}", macro_);
self.offsets.entry(symbol.range.start.line).or_default().push(Offset {
file_id: macro_.file_id,
range: symbol.range,
Expand Down
12 changes: 6 additions & 6 deletions crates/preprocessor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,12 @@ where
}

pub fn preprocess_input(mut self) -> PreprocessingResult {
let _ = (self.include_file)(
&mut self.macros,
"sourcemod".to_string(),
self.file_id,
false,
);
// let _ = (self.include_file)(
// &mut self.macros,
// "sourcemod".to_string(),
// self.file_id,
// false,
// );
let mut col_offset: Option<i32> = None;
let mut expanded_symbol: Option<(Symbol, u32, FileId)> = None;
while let Some(symbol) = if !self.expansion_stack.is_empty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,63 @@ int qux = baz;
"#,
));
}

#[test]
fn preprocessor_disable_1() {
assert_json_snapshot!(goto_definition(
r#"
%! main.sp
#define FOO
#if defined FOO
int foo;
#endif
void bar() {
foo = 1;
|
^
}
"#,
));
}

#[test]
fn preprocessor_disable_2() {
assert_json_snapshot!(goto_definition(
r#"
%! main.sp
#define FOO
#include "foo.sp"
%! foo.sp
#if defined FOO
int foo;
#endif
void bar() {
foo = 1;
|
^
}
"#,
));
}

#[test]
fn preprocessor_disable_3() {
assert_json_snapshot!(goto_definition(
r#"
%! main.sp
#include "foo.sp"
#if defined FOO
int foo;
#endif
void bar() {
foo = 1;
|
^
}
%! foo.sp
#define FOO
"#,
));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
source: crates/sourcepawn_lsp/tests/text_document/goto_definition/macros.rs
assertion_line: 210
expression: "goto_definition(r#\"\n%! main.sp\n#define FOO\n#if defined FOO\nint foo;\n#endif\nvoid bar() {\n foo = 1;\n |\n ^\n}\n\"#)"
---
[
{
"originSelectionRange": {
"start": {
"line": 5,
"character": 4
},
"end": {
"line": 5,
"character": 7
}
},
"targetUri": "file:///main.sp",
"targetRange": {
"start": {
"line": 2,
"character": 4
},
"end": {
"line": 2,
"character": 7
}
},
"targetSelectionRange": {
"start": {
"line": 2,
"character": 4
},
"end": {
"line": 2,
"character": 7
}
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
source: crates/sourcepawn_lsp/tests/text_document/goto_definition/macros.rs
assertion_line: 228
expression: "goto_definition(r#\"\n%! main.sp\n#define FOO\n#include \"foo.sp\"\n\n%! foo.sp\n#if defined FOO\nint foo;\n#endif\nvoid bar() {\n foo = 1;\n |\n ^\n}\n\"#)"
---
[
{
"originSelectionRange": {
"start": {
"line": 4,
"character": 4
},
"end": {
"line": 4,
"character": 7
}
},
"targetUri": "file:///foo.sp",
"targetRange": {
"start": {
"line": 1,
"character": 4
},
"end": {
"line": 1,
"character": 7
}
},
"targetSelectionRange": {
"start": {
"line": 1,
"character": 4
},
"end": {
"line": 1,
"character": 7
}
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
source: crates/sourcepawn_lsp/tests/text_document/goto_definition/macros.rs
assertion_line: 249
expression: "goto_definition(r#\"\n%! main.sp\n#include \"foo.sp\"\n#if defined FOO\nint foo;\n#endif\nvoid bar() {\n foo = 1;\n |\n ^\n}\n\n%! foo.sp\n#define FOO\n\"#)"
---
[
{
"originSelectionRange": {
"start": {
"line": 5,
"character": 4
},
"end": {
"line": 5,
"character": 7
}
},
"targetUri": "file:///main.sp",
"targetRange": {
"start": {
"line": 2,
"character": 4
},
"end": {
"line": 2,
"character": 7
}
},
"targetSelectionRange": {
"start": {
"line": 2,
"character": 4
},
"end": {
"line": 2,
"character": 7
}
}
}
]

0 comments on commit ea347ad

Please sign in to comment.