@@ -48,7 +48,7 @@ Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
48
48
FileManager &FM, SourceManager &SM)
49
49
: Diags(diags), Features(opts), FileMgr(FM), SourceMgr(SM),
50
50
SystemDirIdx(0 ), NoCurDirSearch(false ),
51
- CurLexer(0 ), CurNextDirLookup (0 ), CurMacroExpander(0 ) {
51
+ CurLexer(0 ), CurDirLookup (0 ), CurMacroExpander(0 ) {
52
52
// Clear stats.
53
53
NumDirectives = NumIncluded = NumDefined = NumUndefined = NumPragma = 0 ;
54
54
NumIf = NumElse = NumEndif = 0 ;
@@ -267,11 +267,11 @@ unsigned Preprocessor::getSpelling(const LexerToken &Tok, char *Buffer) const {
267
267
// / return null on failure. isAngled indicates whether the file reference is
268
268
// / for system #include's or not (i.e. using <> instead of "").
269
269
const FileEntry *Preprocessor::LookupFile (const std::string &Filename,
270
- bool isSystem ,
270
+ bool isAngled ,
271
271
const DirectoryLookup *FromDir,
272
- const DirectoryLookup *&NextDir ) {
272
+ const DirectoryLookup *&CurDir ) {
273
273
assert (CurLexer && " Cannot enter a #include inside a macro expansion!" );
274
- NextDir = 0 ;
274
+ CurDir = 0 ;
275
275
276
276
// If 'Filename' is absolute, check to see if it exists and no searching.
277
277
// FIXME: this should be a sys::Path interface, this doesn't handle things
@@ -286,23 +286,28 @@ const FileEntry *Preprocessor::LookupFile(const std::string &Filename,
286
286
287
287
// Step #0, unless disabled, check to see if the file is in the #includer's
288
288
// directory. This search is not done for <> headers.
289
- if (!isSystem && !FromDir && !NoCurDirSearch) {
289
+ if (!isAngled && !FromDir && !NoCurDirSearch) {
290
290
const FileEntry *CurFE =
291
291
SourceMgr.getFileEntryForFileID (CurLexer->getCurFileID ());
292
292
if (CurFE) {
293
+ // Concatenate the requested file onto the directory.
294
+ // FIXME: should be in sys::Path.
293
295
if (const FileEntry *FE =
294
296
FileMgr.getFile (CurFE->getDir ()->getName ()+" /" +Filename)) {
295
- if (CurNextDirLookup )
296
- NextDir = CurNextDirLookup ;
297
+ if (CurDirLookup )
298
+ CurDir = CurDirLookup ;
297
299
else
298
- NextDir = &SearchDirs[0 ];
300
+ CurDir = 0 ;
301
+
302
+ // This file is a system header or C++ unfriendly if the old file is.
303
+ getFileInfo (FE).DirInfo = getFileInfo (CurFE).DirInfo ;
299
304
return FE;
300
305
}
301
306
}
302
307
}
303
308
304
309
// If this is a system #include, ignore the user #include locs.
305
- unsigned i = isSystem ? SystemDirIdx : 0 ;
310
+ unsigned i = isAngled ? SystemDirIdx : 0 ;
306
311
307
312
// If this is a #include_next request, start searching after the directory the
308
313
// file was found in.
@@ -315,7 +320,10 @@ const FileEntry *Preprocessor::LookupFile(const std::string &Filename,
315
320
// FIXME: should be in sys::Path.
316
321
if (const FileEntry *FE =
317
322
FileMgr.getFile (SearchDirs[i].getDir ()->getName ()+" /" +Filename)) {
318
- NextDir = &SearchDirs[i+1 ];
323
+ CurDir = &SearchDirs[i];
324
+
325
+ // This file is a system header or C++ unfriendly if the dir is.
326
+ getFileInfo (FE).DirInfo = CurDir->getDirCharacteristic ();
319
327
return FE;
320
328
}
321
329
}
@@ -328,12 +336,12 @@ const FileEntry *Preprocessor::LookupFile(const std::string &Filename,
328
336
// / start lexing tokens from it instead of the current buffer. Return true
329
337
// / on failure.
330
338
void Preprocessor::EnterSourceFile (unsigned FileID,
331
- const DirectoryLookup *NextDir ) {
339
+ const DirectoryLookup *CurDir ) {
332
340
++NumEnteredSourceFiles;
333
341
334
342
// Add the current lexer to the include stack.
335
343
if (CurLexer) {
336
- IncludeStack.push_back (IncludeStackInfo (CurLexer, CurNextDirLookup ));
344
+ IncludeStack.push_back (IncludeStackInfo (CurLexer, CurDirLookup ));
337
345
} else {
338
346
assert (CurMacroExpander == 0 && " Cannot #include a file inside a macro!" );
339
347
}
@@ -343,12 +351,21 @@ void Preprocessor::EnterSourceFile(unsigned FileID,
343
351
344
352
const SourceBuffer *Buffer = SourceMgr.getBuffer (FileID);
345
353
346
- CurLexer = new Lexer (Buffer, FileID, *this );
347
- CurNextDirLookup = NextDir ;
354
+ CurLexer = new Lexer (Buffer, FileID, *this );
355
+ CurDirLookup = CurDir ;
348
356
349
357
// Notify the client, if desired, that we are in a new source file.
350
- if (FileChangeHandler)
351
- FileChangeHandler (CurLexer->getSourceLocation (CurLexer->BufferStart ), true );
358
+ if (FileChangeHandler) {
359
+ DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
360
+
361
+ // Get the file entry for the current file.
362
+ if (const FileEntry *FE =
363
+ SourceMgr.getFileEntryForFileID (CurLexer->getCurFileID ()))
364
+ FileType = getFileInfo (FE).DirInfo ;
365
+
366
+ FileChangeHandler (CurLexer->getSourceLocation (CurLexer->BufferStart ), true ,
367
+ FileType);
368
+ }
352
369
}
353
370
354
371
// / EnterMacro - Add a Macro to the top of the include stack and start lexing
@@ -357,9 +374,9 @@ void Preprocessor::EnterMacro(LexerToken &Tok) {
357
374
IdentifierTokenInfo *Identifier = Tok.getIdentifierInfo ();
358
375
MacroInfo &MI = *Identifier->getMacroInfo ();
359
376
if (CurLexer) {
360
- IncludeStack.push_back (IncludeStackInfo (CurLexer, CurNextDirLookup ));
361
- CurLexer = 0 ;
362
- CurNextDirLookup = 0 ;
377
+ IncludeStack.push_back (IncludeStackInfo (CurLexer, CurDirLookup ));
378
+ CurLexer = 0 ;
379
+ CurDirLookup = 0 ;
363
380
} else if (CurMacroExpander) {
364
381
MacroStack.push_back (CurMacroExpander);
365
382
}
@@ -492,14 +509,22 @@ void Preprocessor::HandleEndOfFile(LexerToken &Result, bool isEndOfMacro) {
492
509
if (!IncludeStack.empty ()) {
493
510
// We're done with the #included file.
494
511
delete CurLexer;
495
- CurLexer = IncludeStack.back ().TheLexer ;
496
- CurNextDirLookup = IncludeStack.back ().TheDirLookup ;
512
+ CurLexer = IncludeStack.back ().TheLexer ;
513
+ CurDirLookup = IncludeStack.back ().TheDirLookup ;
497
514
IncludeStack.pop_back ();
498
515
499
516
// Notify the client, if desired, that we are in a new source file.
500
- if (FileChangeHandler && !isEndOfMacro)
517
+ if (FileChangeHandler && !isEndOfMacro) {
518
+ DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
519
+
520
+ // Get the file entry for the current file.
521
+ if (const FileEntry *FE =
522
+ SourceMgr.getFileEntryForFileID (CurLexer->getCurFileID ()))
523
+ FileType = getFileInfo (FE).DirInfo ;
524
+
501
525
FileChangeHandler (CurLexer->getSourceLocation (CurLexer->BufferPtr ),
502
- false );
526
+ false , FileType);
527
+ }
503
528
504
529
return Lex (Result);
505
530
}
@@ -923,8 +948,8 @@ void Preprocessor::HandleIncludeDirective(LexerToken &IncludeTok,
923
948
return Diag (FilenameTok, diag::err_pp_empty_filename);
924
949
925
950
// Search include directories.
926
- const DirectoryLookup *NextDir ;
927
- const FileEntry *File = LookupFile (Filename, isAngled, LookupFrom, NextDir );
951
+ const DirectoryLookup *CurDir ;
952
+ const FileEntry *File = LookupFile (Filename, isAngled, LookupFrom, CurDir );
928
953
if (File == 0 )
929
954
return Diag (FilenameTok, diag::err_pp_file_not_found);
930
955
@@ -953,7 +978,7 @@ void Preprocessor::HandleIncludeDirective(LexerToken &IncludeTok,
953
978
return Diag (FilenameTok, diag::err_pp_file_not_found);
954
979
955
980
// Finally, if all is good, enter the new file!
956
- EnterSourceFile (FileID, NextDir );
981
+ EnterSourceFile (FileID, CurDir );
957
982
958
983
// Increment the number of times this file has been included.
959
984
++FileInfo.NumIncludes ;
@@ -967,12 +992,15 @@ void Preprocessor::HandleIncludeNextDirective(LexerToken &IncludeNextTok) {
967
992
// #include_next is like #include, except that we start searching after
968
993
// the current found directory. If we can't do this, issue a
969
994
// diagnostic.
970
- const DirectoryLookup *Lookup = CurNextDirLookup ;
995
+ const DirectoryLookup *Lookup = CurDirLookup ;
971
996
if (IncludeStack.empty ()) {
972
997
Lookup = 0 ;
973
998
Diag (IncludeNextTok, diag::pp_include_next_in_primary);
974
999
} else if (Lookup == 0 ) {
975
1000
Diag (IncludeNextTok, diag::pp_include_next_absolute_path);
1001
+ } else {
1002
+ // Start looking up in the next directory.
1003
+ ++Lookup;
976
1004
}
977
1005
978
1006
return HandleIncludeDirective (IncludeNextTok, Lookup);
0 commit comments