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
use gobject_ast::model::{
ConditionalKind, PragmaKind, PreprocessorDirective, SourceLocation, TopLevelItem,
};
use crate::{
ast_context::AstContext,
config::Config,
rules::{Fix, Rule, Violation},
};
pub struct UsePragmaOnce;
impl Rule for UsePragmaOnce {
fn name(&self) -> &'static str {
"use_pragma_once"
}
fn description(&self) -> &'static str {
"Suggest #pragma once instead of traditional include guards"
}
fn category(&self) -> crate::rules::Category {
crate::rules::Category::Style
}
fn fixable(&self) -> bool {
true
}
fn check_all(
&self,
ast_context: &AstContext,
_config: &Config,
violations: &mut Vec<Violation>,
) {
for (path, file) in ast_context.iter_header_files() {
// Check if the file already uses #pragma once
let has_pragma_once = file.top_level_items.iter().any(|item| {
matches!(
item,
TopLevelItem::Preprocessor(PreprocessorDirective::Pragma {
kind: PragmaKind::Once,
..
})
)
});
if has_pragma_once {
continue; // Already using #pragma once
}
// Look for traditional include guard pattern
if let Some((ifndef_loc, define_loc, endif_loc, guard_name)) =
self.find_include_guard(&file.top_level_items)
{
// Build fixes:
// 1. Replace #ifndef and #define lines with #pragma once
// 2. Remove the #endif line (including any comment) at the end
let mut fixes = Vec::new();
// Fix 1: Delete #ifndef line (with following blank line if any)
let ifndef_fix_loc =
ifndef_loc.with_byte_range(ifndef_loc.start_byte, ifndef_loc.start_byte);
fixes.push(Fix::delete_line_and_trailing_blank(&ifndef_fix_loc));
// Fix 2: Replace #define line with #pragma once
let (define_start, define_end) = define_loc.find_line_bounds();
fixes.push(Fix::new(define_start, define_end, "#pragma once\n"));
// Fix 3: Remove the entire #endif line (with preceding blank line if any)
fixes.push(Fix::delete_line_and_leading_blank(&endif_loc));
violations.push(self.violation_with_fixes_at(
path,
&ifndef_loc,
format!("Use #pragma once instead of include guard '{}'", guard_name),
fixes,
));
}
}
}
}
impl UsePragmaOnce {
/// Find traditional include guard pattern
/// Returns (ifndef_location, define_location, endif_location, guard_name)
fn find_include_guard<'a>(
&self,
items: &'a [TopLevelItem],
) -> Option<(SourceLocation, SourceLocation, SourceLocation, &'a str)> {
// The first non-comment item should be #ifndef (traditional include guard)
items
.iter()
.find(|item| !matches!(item, TopLevelItem::Comment(_)))
.and_then(|item| match item {
TopLevelItem::Preprocessor(PreprocessorDirective::Conditional {
kind: ConditionalKind::Ifndef,
condition: Some(name),
body,
location,
}) => {
// Found #ifndef - check it contains matching #define as first item
let define_loc = self.find_matching_define(body, name)?;
let endif_loc = location.with_byte_range(location.end_byte, location.end_byte);
let ifndef_loc = location.clone();
Some((ifndef_loc, define_loc, endif_loc, name.as_str()))
}
_ => None, // First item is not #ifndef
})
}
/// Find matching #define inside the #ifndef body
/// Returns the define location only if it's a guard (no value) and there's
/// content after it
fn find_matching_define(
&self,
body: &[TopLevelItem],
guard_name: &str,
) -> Option<SourceLocation> {
let non_comment_items: Vec<_> = body
.iter()
.filter(|item| !matches!(item, TopLevelItem::Comment(_)))
.collect();
if non_comment_items.len() < 2 {
return None;
}
non_comment_items
.first()
.and_then(|item| match item {
TopLevelItem::Preprocessor(PreprocessorDirective::Define {
name,
value,
location,
}) => {
if name == guard_name && value.is_none() {
Some(location)
} else {
None
}
}
_ => None,
})
.cloned()
}
}