summaryrefslogtreecommitdiffstats
path: root/clang
diff options
context:
space:
mode:
Diffstat (limited to 'clang')
-rw-r--r--clang/Lex/Lexer.cpp1
-rw-r--r--clang/Lex/Preprocessor.cpp40
-rw-r--r--clang/include/clang/Lex/Lexer.h1
-rw-r--r--clang/include/clang/Lex/Preprocessor.h25
4 files changed, 46 insertions, 21 deletions
diff --git a/clang/Lex/Lexer.cpp b/clang/Lex/Lexer.cpp
index 68ff3777dfc..65b0012cff7 100644
--- a/clang/Lex/Lexer.cpp
+++ b/clang/Lex/Lexer.cpp
@@ -43,6 +43,7 @@ Lexer::Lexer(const SourceBuffer *File, unsigned fileid, Preprocessor &pp,
BufferStart(BufferPtr),
BufferEnd(BufEnd ? BufEnd : File->getBufferEnd()),
InputFile(File), CurFileID(fileid), PP(pp), Features(PP.getLangOptions()) {
+ Is_PragmaLexer = false;
InitCharacterInfo();
assert(BufferEnd[0] == 0 &&
diff --git a/clang/Lex/Preprocessor.cpp b/clang/Lex/Preprocessor.cpp
index 75b28c99dd0..daee3ed7150 100644
--- a/clang/Lex/Preprocessor.cpp
+++ b/clang/Lex/Preprocessor.cpp
@@ -348,6 +348,39 @@ const FileEntry *Preprocessor::LookupFile(const std::string &Filename,
return 0;
}
+/// isInPrimaryFile - Return true if we're in the top-level file, not in a
+/// #include.
+bool Preprocessor::isInPrimaryFile() const {
+ unsigned NumLexersFound = 0;
+ if (CurLexer && !CurLexer->Is_PragmaLexer)
+ ++NumLexersFound;
+
+ /// If there are any stacked lexers, we're in a #include.
+ for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i)
+ if (IncludeMacroStack[i].TheLexer) {
+ if (!IncludeMacroStack[i].TheLexer->Is_PragmaLexer)
+ if (++NumLexersFound > 1)
+ return false;
+ }
+ return NumLexersFound < 2;
+}
+
+/// getCurrentLexer - Return the current file lexer being lexed from. Note
+/// that this ignores any potentially active macro expansions and _Pragma
+/// expansions going on at the time.
+Lexer *Preprocessor::getCurrentFileLexer() const {
+ if (CurLexer && !CurLexer->Is_PragmaLexer) return CurLexer;
+
+ // Look for a stacked lexer.
+ for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
+ Lexer *L = IncludeMacroStack[i].TheLexer;
+ if (L && !L->Is_PragmaLexer) // Ignore macro & _Pragma expansions.
+ return L;
+ }
+ return 0;
+}
+
+
/// EnterSourceFile - Add a source file to the top of the include stack and
/// start lexing tokens from it instead of the current buffer. Return true
/// on failure.
@@ -630,7 +663,7 @@ void Preprocessor::ExpandBuiltinMacro(LexerToken &Tok) {
// Get the file that we are lexing out of. If we're currently lexing from
// a macro, dig into the include stack.
const FileEntry *CurFile = 0;
- Lexer *TheLexer = getCurrentLexer();
+ Lexer *TheLexer = getCurrentFileLexer();
if (TheLexer)
CurFile = SourceMgr.getFileEntryForFileID(TheLexer->getCurFileID());
@@ -1510,6 +1543,9 @@ void Preprocessor::Handle_Pragma(LexerToken &Tok) {
// return an EOM token.
TL->ParsingPreprocessorDirective = true;
+ // This lexer really is for _Pragma.
+ TL->Is_PragmaLexer = true;
+
// With everything set up, lex this as a #pragma directive.
HandlePragmaDirective();
@@ -1619,7 +1655,7 @@ void Preprocessor::HandlePragmaDependency(LexerToken &DependencyTok) {
if (File == 0)
return Diag(FilenameTok, diag::err_pp_file_not_found);
- Lexer *TheLexer = getCurrentLexer();
+ Lexer *TheLexer = getCurrentFileLexer();
const FileEntry *CurFile =
SourceMgr.getFileEntryForFileID(TheLexer->getCurFileID());
diff --git a/clang/include/clang/Lex/Lexer.h b/clang/include/clang/Lex/Lexer.h
index 8b0456e8ac2..bb978c3f36e 100644
--- a/clang/include/clang/Lex/Lexer.h
+++ b/clang/include/clang/Lex/Lexer.h
@@ -58,6 +58,7 @@ class Lexer {
unsigned CurFileID; // FileID for the current input file.
Preprocessor &PP; // Preprocessor object controlling lexing.
LangOptions Features; // Features enabled by this language (cache).
+ bool Is_PragmaLexer; // True if lexer for _Pragma handling.
// Context-specific lexing flags.
bool IsAtStartOfLine; // True if sitting at start of line.
diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h
index ba076f41ce8..a1b2cdc1a43 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -212,26 +212,13 @@ public:
/// isInPrimaryFile - Return true if we're in the top-level file, not in a
/// #include.
- ///
- bool isInPrimaryFile() const {
- /// If there are any stacked lexers, we're in a #include.
- for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i)
- if (IncludeMacroStack[i].TheLexer)
- return false;
- return true;
- }
+ bool isInPrimaryFile() const;
+
+ /// getCurrentLexer - Return the current file lexer being lexed from. Note
+ /// that this ignores any potentially active macro expansions and _Pragma
+ /// expansions going on at the time.
+ Lexer *getCurrentFileLexer() const;
- /// getCurrentLexer - Return the current lexer being lexed from. Note that
- /// this ignores any potentially active macro expansions going on at the time.
- Lexer *getCurrentLexer() const {
- if (CurLexer) return CurLexer;
-
- // Look for a stacked lexer.
- for (unsigned i = IncludeMacroStack.size(); i != 0; --i)
- if (IncludeMacroStack[i].TheLexer) // Ignore macro expansions.
- return IncludeMacroStack[i].TheLexer;
- return 0;
- }
/// SetSearchPaths - Interface for setting the file search paths.
///
OpenPOWER on IntegriCloud