Skip to content

Commit

Permalink
Implement __COUNTER__ preprocessor macro, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
maximecb committed Oct 6, 2023
1 parent f71a842 commit a8aa026
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
25 changes: 22 additions & 3 deletions ncc/src/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ fn parse_def(input: &mut Input) -> Result<Def, ParseError>
fn process_ifdef(
input: &mut Input,
defs: &mut HashMap<String, Def>,
counter: &mut usize,
gen_output: bool,
) -> Result<String, ParseError>
{
Expand All @@ -179,6 +180,7 @@ fn process_ifdef(
process_branches(
input,
defs,
counter,
gen_output,
is_defined,
)
Expand All @@ -187,6 +189,7 @@ fn process_ifdef(
fn process_ifndef(
input: &mut Input,
defs: &mut HashMap<String, Def>,
counter: &mut usize,
gen_output: bool,
) -> Result<String, ParseError>
{
Expand All @@ -196,6 +199,7 @@ fn process_ifndef(
process_branches(
input,
defs,
counter,
gen_output,
!is_defined,
)
Expand All @@ -205,6 +209,7 @@ fn process_ifndef(
fn process_branches(
input: &mut Input,
defs: &mut HashMap<String, Def>,
counter: &mut usize,
gen_output: bool,
branch_cond: bool
) -> Result<String, ParseError>
Expand All @@ -217,6 +222,7 @@ fn process_branches(
let (sub_output, end_keyword) = process_input_rec(
input,
defs,
counter,
gen_output,
)?;

Expand All @@ -225,6 +231,7 @@ fn process_branches(
let (_, end_keyword) = process_input_rec(
input,
defs,
counter,
false,
)?;

Expand All @@ -241,6 +248,7 @@ fn process_branches(
let (_, end_keyword) = process_input_rec(
input,
defs,
counter,
false,
)?;

Expand All @@ -249,6 +257,7 @@ fn process_branches(
let (sub_output, end_keyword) = process_input_rec(
input,
defs,
counter,
gen_output,
)?;

Expand Down Expand Up @@ -312,6 +321,7 @@ fn read_macro_arg(input: &mut Input, depth: usize) -> Result<String, ParseError>
fn expand_macro(
input: &mut Input,
defs: &mut HashMap<String, Def>,
counter: &mut usize,
gen_output: bool,
def: &Def,
) -> Result<String, ParseError>
Expand Down Expand Up @@ -420,6 +430,7 @@ fn expand_macro(
let (sub_input, end_keyword) = process_input_rec(
&mut input,
defs,
counter,
gen_output,
)?;

Expand All @@ -434,10 +445,12 @@ fn expand_macro(
pub fn process_input(input: &mut Input) -> Result<String, ParseError>
{
let mut defs = HashMap::new();
let mut counter = 0;

let (output, end_keyword) = process_input_rec(
input,
&mut defs,
&mut counter,
true,
)?;

Expand All @@ -452,6 +465,7 @@ pub fn process_input(input: &mut Input) -> Result<String, ParseError>
fn process_input_rec(
input: &mut Input,
defs: &mut HashMap<String, Def>,
counter: &mut usize,
gen_output: bool,
) -> Result<(String, String), ParseError>
{
Expand All @@ -476,13 +490,13 @@ fn process_input_rec(

// If defined
if directive == "ifdef" {
output += &process_ifdef(input, defs, gen_output)?;
output += &process_ifdef(input, defs, counter, gen_output)?;
continue
}

// If not defined
if directive == "ifndef" {
output += &process_ifndef(input, defs, gen_output)?;
output += &process_ifndef(input, defs, counter, gen_output)?;
continue
}

Expand All @@ -509,6 +523,7 @@ fn process_input_rec(
let (include_output, end_keyword) = process_input_rec(
&mut include_input,
defs,
counter,
gen_output
)?;

Expand Down Expand Up @@ -577,7 +592,7 @@ fn process_input_rec(
// If we have a definition for this identifier
if let Some(def) = defs.get(&ident) {
let def = def.clone();
output += &expand_macro(input, defs, gen_output, &def)?;
output += &expand_macro(input, defs, counter, gen_output, &def)?;
}
else if ident == "__LINE__" {
output += &format!("{}", input.line_no);
Expand All @@ -589,6 +604,10 @@ fn process_input_rec(
}
output += &filename;
}
else if ident == "__COUNTER__" {
output += &format!("{}", counter);
*counter += 1;
}
else
{
output += &ident;
Expand Down
10 changes: 10 additions & 0 deletions ncc/tests/macros.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#define REC_MACRO2 3
#define REC_MACRO REC_MACRO2

// Definition including counter
#define CTR_MACRO __COUNTER__

// Regression: definition containing a \ character in a string
#define newline_str "\n"

Expand Down Expand Up @@ -67,6 +70,13 @@ void main()
int MACRO2 = 3;
assert(MACRO2 == 3);

// Definition including the counter macro
int c0 = CTR_MACRO;
int c1 = CTR_MACRO;
int c2 = CTR_MACRO;
assert(c1 > c0);
assert(c2 > c1);

// Regression: closing parens inside a string
BAR(")");
BAR("\")\"");
Expand Down

0 comments on commit a8aa026

Please sign in to comment.