blob: 33ccbc0cfc9419195b821b311d00d54822e1e09f [file] [log] [blame]
Ted Kremenek6c90efb2008-11-12 22:43:05 +00001//===--- PreprocessorLexer.cpp - C Language Family Lexer ------------------===//
2//
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"
Ted Kremenek6c90efb2008-11-12 22:43:05 +000018using namespace clang;
19
David Blaikie68e081d2011-12-20 02:48:34 +000020void PreprocessorLexer::anchor() { }
21
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +000022PreprocessorLexer::PreprocessorLexer(Preprocessor *pp, FileID fid)
23 : PP(pp), FID(fid), InitialNumSLocEntries(0),
24 ParsingPreprocessorDirective(false),
Daniel Dunbarcf3f2c42012-11-13 19:12:37 +000025 ParsingFilename(false), LexingRawMode(false) {
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +000026 if (pp)
27 InitialNumSLocEntries = pp->getSourceManager().local_sloc_entry_size();
28}
29
James Dennett6deb8412012-06-17 03:41:54 +000030/// \brief After the preprocessor has parsed a \#include, lex and
Ted Kremenek6c90efb2008-11-12 22:43:05 +000031/// (potentially) macro expand the filename.
32void 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;
Mike Stump11289f42009-09-09 15:08:12 +000039
Ted Kremenek6c90efb2008-11-12 22:43:05 +000040 // Lex the filename.
Eli Friedman0834a4b2013-09-19 00:41:32 +000041 if (LexingRawMode)
42 IndirectLex(FilenameTok);
43 else
44 PP->Lex(FilenameTok);
Ted Kremenek6c90efb2008-11-12 22:43:05 +000045
46 // We should have obtained the filename now.
47 ParsingFilename = false;
Mike Stump11289f42009-09-09 15:08:12 +000048
Ted Kremenek6c90efb2008-11-12 22:43:05 +000049 // No filename?
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +000050 if (FilenameTok.is(tok::eod))
Chris Lattner57dab262008-11-22 06:20:42 +000051 PP->Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
Ted Kremenek6c90efb2008-11-12 22:43:05 +000052}
Chris Lattnerd32480d2009-01-17 06:22:33 +000053
54/// getFileEntry - Return the FileEntry corresponding to this FileID. Like
55/// getFileID(), this only works for lexers with attached preprocessors.
56const FileEntry *PreprocessorLexer::getFileEntry() const {
57 return PP->getSourceManager().getFileEntryForID(getFileID());
58}