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