1
- use crate :: utils:: { ErrAction , File , expect_action, walk_dir_no_dot_or_target} ;
1
+ use crate :: source_map:: { SourceFile , SourceMap , Span } ;
2
+ use crate :: utils:: { ErrAction , expect_action, walk_dir_no_dot_or_target} ;
2
3
use core:: ops:: Range ;
3
4
use core:: slice;
4
5
use rustc_data_structures:: fx:: FxHashMap ;
5
6
use rustc_lexer:: { self as lexer, FrontmatterAllowed } ;
6
7
use std:: fs;
7
- use std:: path:: { self , Path , PathBuf } ;
8
+ use std:: path:: { self , Path } ;
8
9
9
10
#[ derive( Clone , Copy ) ]
10
11
pub enum Token < ' a > {
@@ -184,11 +185,8 @@ impl<'txt> RustSearcher<'txt> {
184
185
}
185
186
186
187
pub struct ActiveLint {
187
- pub krate : String ,
188
- pub module : String ,
189
188
pub group : String ,
190
- pub path : PathBuf ,
191
- pub span : Range < u32 > ,
189
+ pub span : Span ,
192
190
}
193
191
194
192
pub struct DeprecatedLint {
@@ -207,24 +205,26 @@ pub enum Lint {
207
205
Renamed ( RenamedLint ) ,
208
206
}
209
207
210
- pub struct LintList {
208
+ pub struct ParsedData {
209
+ pub source_map : SourceMap ,
211
210
pub lints : FxHashMap < String , Lint > ,
212
211
pub deprecated_span : Range < u32 > ,
213
212
pub renamed_span : Range < u32 > ,
214
213
}
215
- impl LintList {
214
+ impl ParsedData {
216
215
pub fn collect ( ) -> Self {
217
216
// 2025-05: Initial capacities should fit everything without reallocating.
218
217
let mut parser = Parser {
218
+ source_map : SourceMap :: with_capacity ( 8 , 1000 ) ,
219
219
lints : FxHashMap :: with_capacity_and_hasher ( 1000 , Default :: default ( ) ) ,
220
220
deprecated_span : 0 ..0 ,
221
221
renamed_span : 0 ..0 ,
222
- contents : String :: with_capacity ( 1024 * 128 * 3 ) ,
223
222
} ;
224
223
parser. parse_src_files ( ) ;
225
224
parser. parse_deprecated_lints ( ) ;
226
225
227
- LintList {
226
+ ParsedData {
227
+ source_map : parser. source_map ,
228
228
lints : parser. lints ,
229
229
deprecated_span : parser. deprecated_span ,
230
230
renamed_span : parser. renamed_span ,
@@ -233,10 +233,10 @@ impl LintList {
233
233
}
234
234
235
235
struct Parser {
236
+ source_map : SourceMap ,
236
237
lints : FxHashMap < String , Lint > ,
237
238
deprecated_span : Range < u32 > ,
238
239
renamed_span : Range < u32 > ,
239
- contents : String ,
240
240
}
241
241
impl Parser {
242
242
/// Parses all source files looking for lint declarations (`declare_clippy_lint! { .. }`).
@@ -250,8 +250,9 @@ impl Parser {
250
250
continue ;
251
251
} ;
252
252
if crate_path. starts_with ( "clippy_lints" ) && crate_path != "clippy_lints_internal" {
253
- crate_path. push_str ( "/src" ) ;
254
- let krate = & crate_path[ ..crate_path. len ( ) - 4 ] ;
253
+ let krate = self . source_map . add_new_crate ( & crate_path) ;
254
+ crate_path. push ( path:: MAIN_SEPARATOR ) ;
255
+ crate_path. push_str ( "src" ) ;
255
256
for e in walk_dir_no_dot_or_target ( & crate_path) {
256
257
let e = expect_action ( e, ErrAction :: Read , & crate_path) ;
257
258
if let Some ( path) = e. path ( ) . to_str ( )
@@ -270,15 +271,16 @@ impl Parser {
270
271
} ;
271
272
path. replace ( path:: MAIN_SEPARATOR , "::" )
272
273
} ;
273
- self . parse_src_file ( e. path ( ) , krate, & module) ;
274
+ let file = self . source_map . load_new_file ( e. path ( ) , krate, module) ;
275
+ self . parse_src_file ( file) ;
274
276
}
275
277
}
276
278
}
277
279
}
278
280
}
279
281
280
282
/// Parse a source file looking for `declare_clippy_lint` macro invocations.
281
- fn parse_src_file ( & mut self , path : & Path , krate : & str , module : & str ) {
283
+ fn parse_src_file ( & mut self , file : SourceFile ) {
282
284
#[ allow( clippy:: enum_glob_use) ]
283
285
use Token :: * ;
284
286
#[ rustfmt:: skip]
@@ -291,20 +293,20 @@ impl Parser {
291
293
Ident ( "pub" ) , CaptureIdent , Comma , AnyComment , CaptureIdent , Comma ,
292
294
] ;
293
295
294
- File :: open_read_to_cleared_string ( path, & mut self . contents ) ;
295
- let mut searcher = RustSearcher :: new ( & self . contents ) ;
296
+ let mut searcher = RustSearcher :: new ( & self . source_map . files [ file] . contents ) ;
296
297
while searcher. find_token ( Ident ( "declare_clippy_lint" ) ) {
297
298
let start = searcher. pos ( ) - "declare_clippy_lint" . len ( ) as u32 ;
298
299
let ( mut name, mut group) = ( "" , "" ) ;
299
300
if searcher. match_tokens ( DECL_TOKENS , & mut [ & mut name, & mut group] ) && searcher. find_token ( CloseBrace ) {
300
301
self . lints . insert (
301
302
name. to_ascii_lowercase ( ) ,
302
303
Lint :: Active ( ActiveLint {
303
- krate : krate. into ( ) ,
304
- module : module. into ( ) ,
305
304
group : group. into ( ) ,
306
- path : path. into ( ) ,
307
- span : start..searcher. pos ( ) ,
305
+ span : Span {
306
+ file,
307
+ start,
308
+ end : searcher. pos ( ) ,
309
+ } ,
308
310
} ) ,
309
311
) ;
310
312
}
@@ -332,10 +334,15 @@ impl Parser {
332
334
Bang , OpenBrace , Ident ( "RENAMED" ) , OpenParen , Ident ( "RENAMED_VERSION" ) , CloseParen , Eq , OpenBracket ,
333
335
] ;
334
336
335
- let path = Path :: new ( "clippy_lints/src/deprecated_lints.rs" ) ;
336
- File :: open_read_to_cleared_string ( path, & mut self . contents ) ;
337
+ let krate = self . source_map . add_crate ( "clippy_lints" ) ;
338
+ let file = self . source_map . load_file (
339
+ Path :: new ( "clippy_lints/src/deprecated_lints.rs" ) ,
340
+ krate,
341
+ "deprecated_lints" ,
342
+ ) ;
343
+ let file = & self . source_map . files [ file] ;
337
344
338
- let mut searcher = RustSearcher :: new ( & self . contents ) ;
345
+ let mut searcher = RustSearcher :: new ( & file . contents ) ;
339
346
// First instance is the macro definition.
340
347
assert ! (
341
348
searcher. find_token( Ident ( "declare_with_version" ) ) ,
@@ -350,10 +357,10 @@ impl Parser {
350
357
let mut reason = "" ;
351
358
while searcher. match_tokens ( DECL_TOKENS , & mut [ & mut version, & mut name, & mut reason] ) {
352
359
self . lints . insert (
353
- parse_clippy_lint_name ( path, name) ,
360
+ parse_clippy_lint_name ( & file . path , name) ,
354
361
Lint :: Deprecated ( DeprecatedLint {
355
- reason : parse_str_single_line ( path, reason) ,
356
- version : parse_str_single_line ( path, version) ,
362
+ reason : parse_str_single_line ( & file . path , reason) ,
363
+ version : parse_str_single_line ( & file . path , version) ,
357
364
} ) ,
358
365
) ;
359
366
end = searcher. pos ( ) ;
@@ -371,10 +378,10 @@ impl Parser {
371
378
let mut new_name = "" ;
372
379
while searcher. match_tokens ( DECL_TOKENS , & mut [ & mut version, & mut old_name, & mut new_name] ) {
373
380
self . lints . insert (
374
- parse_clippy_lint_name ( path, old_name) ,
381
+ parse_clippy_lint_name ( & file . path , old_name) ,
375
382
Lint :: Renamed ( RenamedLint {
376
- new_name : parse_maybe_clippy_lint_name ( path, new_name) ,
377
- version : parse_str_single_line ( path, version) ,
383
+ new_name : parse_maybe_clippy_lint_name ( & file . path , new_name) ,
384
+ version : parse_str_single_line ( & file . path , version) ,
378
385
} ) ,
379
386
) ;
380
387
end = searcher. pos ( ) ;
0 commit comments