1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//! AST (Abstract Syntax Tree) for GNU Makefiles
//!
//! This module defines the AST structure for representing parsed Makefiles.
//! The design follows the specification in:
//! `docs/specification/lint-purify-test-write-Makefile-document-gnu-guide.md`
use std::fmt;
/// Root AST node representing a complete Makefile
#[derive(Debug, Clone, PartialEq)]
pub struct MakeAst {
/// All items in the Makefile (targets, variables, conditionals, etc.)
pub items: Vec<MakeItem>,
/// Metadata about the Makefile
pub metadata: MakeMetadata,
}
/// Metadata about the parsed Makefile
#[derive(Debug, Clone, PartialEq)]
pub struct MakeMetadata {
/// Source file path (if available)
pub source_file: Option<String>,
/// Number of lines in the source
pub line_count: usize,
/// Parse time in milliseconds
pub parse_time_ms: u64,
}
impl MakeMetadata {
/// Create default metadata
pub fn new() -> Self {
Self {
source_file: None,
line_count: 0,
parse_time_ms: 0,
}
}
/// Create metadata with line count
pub fn with_line_count(line_count: usize) -> Self {
Self {
source_file: None,
line_count,
parse_time_ms: 0,
}
}
}
impl Default for MakeMetadata {
fn default() -> Self {
Self::new()
}
}
/// Metadata about recipe formatting (line continuations, etc.)
#[derive(Debug, Clone, PartialEq)]
pub struct RecipeMetadata {
/// Original line breaks in the recipe (indices where continuations occurred)
/// Each entry contains:
/// - character position in the concatenated recipe line
/// - original indentation of the continued line
pub line_breaks: Vec<(usize, String)>,
}
impl RecipeMetadata {
/// Create empty recipe metadata
pub fn new() -> Self {
Self {
line_breaks: Vec::new(),
}
}
/// Create metadata with line breaks
pub fn with_breaks(line_breaks: Vec<(usize, String)>) -> Self {
Self { line_breaks }
}
}
impl Default for RecipeMetadata {
fn default() -> Self {
Self::new()
}
}
/// Makefile constructs (targets, variables, conditionals, etc.)
#[derive(Debug, Clone, PartialEq)]
pub enum MakeItem {
/// A target with prerequisites and recipe
///
/// Example:
/// ```makefile
/// build: src/main.c src/util.c
/// gcc -o build src/main.c src/util.c
/// ```
Target {
/// Target name (e.g., "build", "test", "clean")
name: String,
/// List of prerequisites (targets or files this depends on)
prerequisites: Vec<String>,
/// Recipe lines (commands to execute, tab-indented)
recipe: Vec<String>,
/// Whether this target is marked as .PHONY
phony: bool,
/// Recipe formatting metadata (line continuations, etc.)
recipe_metadata: Option<RecipeMetadata>,
/// Source location
span: Span,
},
/// A variable assignment
///
/// Example:
/// ```makefile
/// CC := gcc
/// CFLAGS = -O2 -Wall
/// ```
Variable {
/// Variable name
name: String,
/// Variable value
value: String,
/// Variable flavor (=, :=, ?=, +=, !=)
flavor: VarFlavor,
/// Source location
span: Span,
},
/// A pattern rule
///
/// Example:
/// ```makefile
/// %.o: %.c
/// $(CC) -c $< -o $@
/// ```
PatternRule {
/// Target pattern (e.g., "%.o")
target_pattern: String,
/// Prerequisite patterns
prereq_patterns: Vec<String>,
/// Recipe lines
recipe: Vec<String>,
/// Recipe formatting metadata (line continuations, etc.)
recipe_metadata: Option<RecipeMetadata>,
/// Source location
span: Span,
},
/// A conditional block (ifeq, ifdef, etc.)
///
/// Example:
/// ```makefile
/// ifeq ($(DEBUG),1)
/// CFLAGS = -g
/// else
/// CFLAGS = -O2
/// endif
/// ```
Conditional {
/// Condition type
condition: MakeCondition,
/// Items in the "then" branch
then_items: Vec<MakeItem>,
/// Items in the "else" branch (if present)
else_items: Option<Vec<MakeItem>>,
/// Source location
span: Span,
},
/// An include directive
///
/// Example:
/// ```makefile
/// include common.mk
/// -include optional.mk
/// ```
Include {
/// File path to include
path: String,
/// Whether this is optional (-include)
optional: bool,
/// Source location
span: Span,
},
/// A function call
///
/// Example:
/// ```makefile
/// SOURCES := $(wildcard src/*.c)
/// OBJS := $(patsubst %.c,%.o,$(SOURCES))
/// ```
FunctionCall {
/// Function name (e.g., "wildcard", "patsubst")
name: String,
/// Function arguments
args: Vec<String>,
/// Source location
span: Span,
},
/// A comment line
///
/// Example:
/// ```makefile
/// # This is a comment
/// ```
Comment {
/// Comment text (without the # prefix)
text: String,
/// Source location
span: Span,
},
}
/// Variable assignment flavors
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VarFlavor {
/// Recursive assignment (=) - expanded when used
Recursive,
/// Simple assignment (:=) - expanded immediately (PREFERRED)
Simple,
/// Conditional assignment (?=) - only if not already defined
Conditional,
/// Append (+=) - add to existing value
Append,
/// Shell assignment (!=) - execute shell command
Shell,
}
impl fmt::Display for VarFlavor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
VarFlavor::Recursive => write!(f, "="),
VarFlavor::Simple => write!(f, ":="),
VarFlavor::Conditional => write!(f, "?="),
VarFlavor::Append => write!(f, "+="),
VarFlavor::Shell => write!(f, "!="),
}
}
}
/// Conditional types in Makefiles
#[derive(Debug, Clone, PartialEq, Eq)]
/// Makefile conditional directives - names match Make syntax exactly
#[allow(clippy::enum_variant_names)]
pub enum MakeCondition {
/// ifeq ($(VAR),value)
IfEq(String, String),
/// ifneq ($(VAR),value)
IfNeq(String, String),
/// ifdef VAR
IfDef(String),
/// ifndef VAR
IfNdef(String),
}
/// Source location information
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
pub struct Span {
/// Start byte offset
pub start: usize,
/// End byte offset
pub end: usize,
/// Line number (1-indexed)
pub line: usize,
}
impl Span {
/// Create a dummy span (for testing or when location is unknown)
pub fn dummy() -> Self {
Span {
start: 0,
end: 0,
line: 0,
}
}
/// Create a span with specific values
pub fn new(start: usize, end: usize, line: usize) -> Self {
Span { start, end, line }
}
}
impl Default for Span {
fn default() -> Self {
Self::dummy()
}
}
#[cfg(test)]
#[path = "ast_tests_var_flavor.rs"]
mod tests_extracted;