comparison clang/lib/Lex/PPLexerChange.cpp @ 252:1f2b6ac9f198 llvm-original

LLVM16-1
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Fri, 18 Aug 2023 09:04:13 +0900
parents c4bab56944e8
children 731cecd08f47
comparison
equal deleted inserted replaced
237:c80f45b162ad 252:1f2b6ac9f198
20 #include "clang/Lex/PreprocessorOptions.h" 20 #include "clang/Lex/PreprocessorOptions.h"
21 #include "llvm/ADT/StringSwitch.h" 21 #include "llvm/ADT/StringSwitch.h"
22 #include "llvm/Support/FileSystem.h" 22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/MemoryBufferRef.h" 23 #include "llvm/Support/MemoryBufferRef.h"
24 #include "llvm/Support/Path.h" 24 #include "llvm/Support/Path.h"
25 #include <optional>
25 26
26 using namespace clang; 27 using namespace clang;
27 28
28 //===----------------------------------------------------------------------===// 29 //===----------------------------------------------------------------------===//
29 // Miscellaneous Methods. 30 // Miscellaneous Methods.
73 74
74 if (MaxIncludeStackDepth < IncludeMacroStack.size()) 75 if (MaxIncludeStackDepth < IncludeMacroStack.size())
75 MaxIncludeStackDepth = IncludeMacroStack.size(); 76 MaxIncludeStackDepth = IncludeMacroStack.size();
76 77
77 // Get the MemoryBuffer for this FID, if it fails, we fail. 78 // Get the MemoryBuffer for this FID, if it fails, we fail.
78 llvm::Optional<llvm::MemoryBufferRef> InputFile = 79 std::optional<llvm::MemoryBufferRef> InputFile =
79 getSourceManager().getBufferOrNone(FID, Loc); 80 getSourceManager().getBufferOrNone(FID, Loc);
80 if (!InputFile) { 81 if (!InputFile) {
81 SourceLocation FileStart = SourceMgr.getLocForStartOfFile(FID); 82 SourceLocation FileStart = SourceMgr.getLocForStartOfFile(FID);
82 Diag(Loc, diag::err_pp_error_opening_file) 83 Diag(Loc, diag::err_pp_error_opening_file)
83 << std::string(SourceMgr.getBufferName(FileStart)) << ""; 84 << std::string(SourceMgr.getBufferName(FileStart)) << "";
92 } 93 }
93 94
94 Lexer *TheLexer = new Lexer(FID, *InputFile, *this, IsFirstIncludeOfFile); 95 Lexer *TheLexer = new Lexer(FID, *InputFile, *this, IsFirstIncludeOfFile);
95 if (getPreprocessorOpts().DependencyDirectivesForFile && 96 if (getPreprocessorOpts().DependencyDirectivesForFile &&
96 FID != PredefinesFileID) { 97 FID != PredefinesFileID) {
97 if (Optional<FileEntryRef> File = SourceMgr.getFileEntryRefForID(FID)) { 98 if (OptionalFileEntryRef File = SourceMgr.getFileEntryRefForID(FID)) {
98 if (Optional<ArrayRef<dependency_directives_scan::Directive>> 99 if (std::optional<ArrayRef<dependency_directives_scan::Directive>>
99 DepDirectives = 100 DepDirectives =
100 getPreprocessorOpts().DependencyDirectivesForFile(*File)) { 101 getPreprocessorOpts().DependencyDirectivesForFile(*File)) {
101 TheLexer->DepDirectives = *DepDirectives; 102 TheLexer->DepDirectives = *DepDirectives;
102 } 103 }
103 } 104 }
220 } 221 }
221 222
222 /// Compute the relative path that names the given file relative to 223 /// Compute the relative path that names the given file relative to
223 /// the given directory. 224 /// the given directory.
224 static void computeRelativePath(FileManager &FM, const DirectoryEntry *Dir, 225 static void computeRelativePath(FileManager &FM, const DirectoryEntry *Dir,
225 const FileEntry *File, 226 FileEntryRef File, SmallString<128> &Result) {
226 SmallString<128> &Result) {
227 Result.clear(); 227 Result.clear();
228 228
229 StringRef FilePath = File->getDir()->getName(); 229 StringRef FilePath = File.getDir().getName();
230 StringRef Path = FilePath; 230 StringRef Path = FilePath;
231 while (!Path.empty()) { 231 while (!Path.empty()) {
232 if (auto CurDir = FM.getDirectory(Path)) { 232 if (auto CurDir = FM.getDirectory(Path)) {
233 if (*CurDir == Dir) { 233 if (*CurDir == Dir) {
234 Result = FilePath.substr(Path.size()); 234 Result = FilePath.substr(Path.size());
235 llvm::sys::path::append(Result, 235 llvm::sys::path::append(Result,
236 llvm::sys::path::filename(File->getName())); 236 llvm::sys::path::filename(File.getName()));
237 return; 237 return;
238 } 238 }
239 } 239 }
240 240
241 Path = llvm::sys::path::parent_path(Path); 241 Path = llvm::sys::path::parent_path(Path);
242 } 242 }
243 243
244 Result = File->getName(); 244 Result = File.getName();
245 } 245 }
246 246
247 void Preprocessor::PropagateLineStartLeadingSpaceInfo(Token &Result) { 247 void Preprocessor::PropagateLineStartLeadingSpaceInfo(Token &Result) {
248 if (CurTokenLexer) { 248 if (CurTokenLexer) {
249 CurTokenLexer->PropagateLineStartLeadingSpaceInfo(Result); 249 CurTokenLexer->PropagateLineStartLeadingSpaceInfo(Result);
279 return EndPos; 279 return EndPos;
280 } 280 }
281 281
282 static void collectAllSubModulesWithUmbrellaHeader( 282 static void collectAllSubModulesWithUmbrellaHeader(
283 const Module &Mod, SmallVectorImpl<const Module *> &SubMods) { 283 const Module &Mod, SmallVectorImpl<const Module *> &SubMods) {
284 if (Mod.getUmbrellaHeader()) 284 if (Mod.getUmbrellaHeaderAsWritten())
285 SubMods.push_back(&Mod); 285 SubMods.push_back(&Mod);
286 for (auto *M : Mod.submodules()) 286 for (auto *M : Mod.submodules())
287 collectAllSubModulesWithUmbrellaHeader(*M, SubMods); 287 collectAllSubModulesWithUmbrellaHeader(*M, SubMods);
288 } 288 }
289 289
290 void Preprocessor::diagnoseMissingHeaderInUmbrellaDir(const Module &Mod) { 290 void Preprocessor::diagnoseMissingHeaderInUmbrellaDir(const Module &Mod) {
291 const Module::Header &UmbrellaHeader = Mod.getUmbrellaHeader(); 291 std::optional<Module::Header> UmbrellaHeader =
292 assert(UmbrellaHeader.Entry && "Module must use umbrella header"); 292 Mod.getUmbrellaHeaderAsWritten();
293 const FileID &File = SourceMgr.translateFile(UmbrellaHeader.Entry); 293 assert(UmbrellaHeader && "Module must use umbrella header");
294 const FileID &File = SourceMgr.translateFile(UmbrellaHeader->Entry);
294 SourceLocation ExpectedHeadersLoc = SourceMgr.getLocForEndOfFile(File); 295 SourceLocation ExpectedHeadersLoc = SourceMgr.getLocForEndOfFile(File);
295 if (getDiagnostics().isIgnored(diag::warn_uncovered_module_header, 296 if (getDiagnostics().isIgnored(diag::warn_uncovered_module_header,
296 ExpectedHeadersLoc)) 297 ExpectedHeadersLoc))
297 return; 298 return;
298 299
299 ModuleMap &ModMap = getHeaderSearchInfo().getModuleMap(); 300 ModuleMap &ModMap = getHeaderSearchInfo().getModuleMap();
300 const DirectoryEntry *Dir = Mod.getUmbrellaDir().Entry; 301 OptionalDirectoryEntryRef Dir = Mod.getEffectiveUmbrellaDir();
301 llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem(); 302 llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
302 std::error_code EC; 303 std::error_code EC;
303 for (llvm::vfs::recursive_directory_iterator Entry(FS, Dir->getName(), EC), 304 for (llvm::vfs::recursive_directory_iterator Entry(FS, Dir->getName(), EC),
304 End; 305 End;
305 Entry != End && !EC; Entry.increment(EC)) { 306 Entry != End && !EC; Entry.increment(EC)) {
310 if (!StringSwitch<bool>(llvm::sys::path::extension(Entry->path())) 311 if (!StringSwitch<bool>(llvm::sys::path::extension(Entry->path()))
311 .Cases(".h", ".H", ".hh", ".hpp", true) 312 .Cases(".h", ".H", ".hh", ".hpp", true)
312 .Default(false)) 313 .Default(false))
313 continue; 314 continue;
314 315
315 if (auto Header = getFileManager().getFile(Entry->path())) 316 if (auto Header = getFileManager().getOptionalFileRef(Entry->path()))
316 if (!getSourceManager().hasFileInfo(*Header)) { 317 if (!getSourceManager().hasFileInfo(*Header)) {
317 if (!ModMap.isHeaderInUnavailableModule(*Header)) { 318 if (!ModMap.isHeaderInUnavailableModule(*Header)) {
318 // Find the relative path that would access this header. 319 // Find the relative path that would access this header.
319 SmallString<128> RelativePath; 320 SmallString<128> RelativePath;
320 computeRelativePath(FileMgr, Dir, *Header, RelativePath); 321 computeRelativePath(FileMgr, *Dir, *Header, RelativePath);
321 Diag(ExpectedHeadersLoc, diag::warn_uncovered_module_header) 322 Diag(ExpectedHeadersLoc, diag::warn_uncovered_module_header)
322 << Mod.getFullModuleName() << RelativePath; 323 << Mod.getFullModuleName() << RelativePath;
323 } 324 }
324 } 325 }
325 } 326 }
330 /// the include stack and keeps going. 331 /// the include stack and keeps going.
331 bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) { 332 bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
332 assert(!CurTokenLexer && 333 assert(!CurTokenLexer &&
333 "Ending a file when currently in a macro!"); 334 "Ending a file when currently in a macro!");
334 335
336 SourceLocation UnclosedSafeBufferOptOutLoc;
337
338 if (IncludeMacroStack.empty() &&
339 isPPInSafeBufferOptOutRegion(UnclosedSafeBufferOptOutLoc)) {
340 // To warn if a "-Wunsafe-buffer-usage" opt-out region is still open by the
341 // end of a file.
342 Diag(UnclosedSafeBufferOptOutLoc,
343 diag::err_pp_unclosed_pragma_unsafe_buffer_usage);
344 }
335 // If we have an unclosed module region from a pragma at the end of a 345 // If we have an unclosed module region from a pragma at the end of a
336 // module, complain and close it now. 346 // module, complain and close it now.
337 const bool LeavingSubmodule = CurLexer && CurLexerSubmodule; 347 const bool LeavingSubmodule = CurLexer && CurLexerSubmodule;
338 if ((LeavingSubmodule || IncludeMacroStack.empty()) && 348 if ((LeavingSubmodule || IncludeMacroStack.empty()) &&
339 !BuildingSubmoduleStack.empty() && 349 !BuildingSubmoduleStack.empty() &&
523 } else { 533 } else {
524 // Client should lex another token unless we generated an EOM. 534 // Client should lex another token unless we generated an EOM.
525 return LeavingSubmodule; 535 return LeavingSubmodule;
526 } 536 }
527 } 537 }
528
529 // If this is the end of the main file, form an EOF token. 538 // If this is the end of the main file, form an EOF token.
530 assert(CurLexer && "Got EOF but no current lexer set!"); 539 assert(CurLexer && "Got EOF but no current lexer set!");
531 const char *EndPos = getCurLexerEndPos(); 540 const char *EndPos = getCurLexerEndPos();
532 Result.startToken(); 541 Result.startToken();
533 CurLexer->BufferPtr = EndPos; 542 CurLexer->BufferPtr = EndPos;
534 CurLexer->FormTokenWithChars(Result, EndPos, tok::eof); 543
544 if (isIncrementalProcessingEnabled()) {
545 CurLexer->FormTokenWithChars(Result, EndPos, tok::annot_repl_input_end);
546 Result.setAnnotationEndLoc(Result.getLocation());
547 Result.setAnnotationValue(nullptr);
548 } else {
549 CurLexer->FormTokenWithChars(Result, EndPos, tok::eof);
550 }
535 551
536 if (isCodeCompletionEnabled()) { 552 if (isCodeCompletionEnabled()) {
537 // Inserting the code-completion point increases the source buffer by 1, 553 // Inserting the code-completion point increases the source buffer by 1,
538 // but the main FileID was created before inserting the point. 554 // but the main FileID was created before inserting the point.
539 // Compensate by reducing the EOF location by 1, otherwise the location 555 // Compensate by reducing the EOF location by 1, otherwise the location