blob: 9f930c3a3c6a0dedbe286330c7bd56e1a09a8c7a [file] [log] [blame]
Eugene Zelenko5dc60fe2017-12-04 23:16:21 +00001//===- PreprocessorLexer.cpp - C Language Family Lexer --------------------===//
Ted Kremenek6c90efb2008-11-12 22:43:05 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the PreprocessorLexer and Token interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Lex/PreprocessorLexer.h"
Ted Kremenek6c90efb2008-11-12 22:43:05 +000015#include "clang/Basic/SourceManager.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/Lex/LexDiagnostic.h"
17#include "clang/Lex/Preprocessor.h"
Eugene Zelenko5dc60fe2017-12-04 23:16:21 +000018#include "clang/Lex/Token.h"
19#include <cassert>
20
Ted Kremenek6c90efb2008-11-12 22:43:05 +000021using namespace clang;
22
Eugene Zelenko5dc60fe2017-12-04 23:16:21 +000023void PreprocessorLexer::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +000024
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +000025PreprocessorLexer::PreprocessorLexer(Preprocessor *pp, FileID fid)
Eugene Zelenko5dc60fe2017-12-04 23:16:21 +000026 : PP(pp), FID(fid) {
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +000027 if (pp)
28 InitialNumSLocEntries = pp->getSourceManager().local_sloc_entry_size();
29}
30
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000031/// After the preprocessor has parsed a \#include, lex and
Ted Kremenek6c90efb2008-11-12 22:43:05 +000032/// (potentially) macro expand the filename.
33void PreprocessorLexer::LexIncludeFilename(Token &FilenameTok) {
34 assert(ParsingPreprocessorDirective &&
35 ParsingFilename == false &&
36 "Must be in a preprocessing directive!");
37
38 // We are now parsing a filename!
39 ParsingFilename = true;
Mike Stump11289f42009-09-09 15:08:12 +000040
Ted Kremenek6c90efb2008-11-12 22:43:05 +000041 // Lex the filename.
Eli Friedman0834a4b2013-09-19 00:41:32 +000042 if (LexingRawMode)
43 IndirectLex(FilenameTok);
44 else
45 PP->Lex(FilenameTok);
Ted Kremenek6c90efb2008-11-12 22:43:05 +000046
47 // We should have obtained the filename now.
48 ParsingFilename = false;
Mike Stump11289f42009-09-09 15:08:12 +000049
Ted Kremenek6c90efb2008-11-12 22:43:05 +000050 // No filename?
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +000051 if (FilenameTok.is(tok::eod))
Chris Lattner57dab262008-11-22 06:20:42 +000052 PP->Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
Ted Kremenek6c90efb2008-11-12 22:43:05 +000053}
Chris Lattnerd32480d2009-01-17 06:22:33 +000054
55/// getFileEntry - Return the FileEntry corresponding to this FileID. Like
56/// getFileID(), this only works for lexers with attached preprocessors.
57const FileEntry *PreprocessorLexer::getFileEntry() const {
58 return PP->getSourceManager().getFileEntryForID(getFileID());
59}