1 //===- PreprocessorLexer.cpp - C Language Family Lexer --------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the PreprocessorLexer and Token interfaces. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/Lex/PreprocessorLexer.h" 14 #include "clang/Basic/SourceManager.h" 15 #include "clang/Lex/LexDiagnostic.h" 16 #include "clang/Lex/Preprocessor.h" 17 #include "clang/Lex/Token.h" 18 #include <cassert> 19 20 using namespace clang; 21 22 void PreprocessorLexer::anchor() {} 23 24 PreprocessorLexer::PreprocessorLexer(Preprocessor *pp, FileID fid) 25 : PP(pp), FID(fid) { 26 if (pp) 27 InitialNumSLocEntries = pp->getSourceManager().local_sloc_entry_size(); 28 } 29 30 /// After the preprocessor has parsed a \#include, lex and 31 /// (potentially) macro expand the filename. 32 void PreprocessorLexer::LexIncludeFilename(Token &FilenameTok) { 33 assert(ParsingPreprocessorDirective && 34 ParsingFilename == false && 35 "Must be in a preprocessing directive!"); 36 37 // We are now parsing a filename! 38 ParsingFilename = true; 39 40 // Lex the filename. 41 if (LexingRawMode) 42 IndirectLex(FilenameTok); 43 else 44 PP->Lex(FilenameTok); 45 46 // We should have obtained the filename now. 47 ParsingFilename = false; 48 49 // No filename? 50 if (FilenameTok.is(tok::eod)) 51 PP->Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename); 52 } 53 54 /// getFileEntry - Return the FileEntry corresponding to this FileID. Like 55 /// getFileID(), this only works for lexers with attached preprocessors. 56 const FileEntry *PreprocessorLexer::getFileEntry() const { 57 return PP->getSourceManager().getFileEntryForID(getFileID()); 58 } 59