zcov: / lib/Lex/PPLexerChange.cpp


Files: 1 Branches Taken: 73.9% 99 / 134
Generated: 2010-02-10 01:31 Branches Executed: 91.0% 122 / 134
Line Coverage: 93.5% 129 / 138


Programs: 2 Runs 3018


       1                 : //===--- PPLexerChange.cpp - Handle changing lexers in the preprocessor ---===//
       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 pieces of the Preprocessor interface that manage the
      11                 : // current lexer stack.
      12                 : //
      13                 : //===----------------------------------------------------------------------===//
      14                 : 
      15                 : #include "clang/Lex/Preprocessor.h"
      16                 : #include "clang/Lex/HeaderSearch.h"
      17                 : #include "clang/Lex/MacroInfo.h"
      18                 : #include "clang/Lex/LexDiagnostic.h"
      19                 : #include "clang/Basic/SourceManager.h"
      20                 : #include "llvm/Support/MemoryBuffer.h"
      21                 : using namespace clang;
      22                 : 
                      204: branch 0 taken
                      204: branch 1 taken
                        0: branch 3 not taken
                        0: branch 4 not taken
                        0: branch 6 not taken
                      204: branch 7 taken
      23              204: PPCallbacks::~PPCallbacks() {}
      24                 : 
      25                 : //===----------------------------------------------------------------------===//
      26                 : // Miscellaneous Methods.
      27                 : //===----------------------------------------------------------------------===//
      28                 : 
      29                 : /// isInPrimaryFile - Return true if we're in the top-level file, not in a
      30                 : /// #include.  This looks through macro expansions and active _Pragma lexers.
      31           269991: bool Preprocessor::isInPrimaryFile() const {
                   269990: branch 1 taken
                        1: branch 2 taken
      32           269991:   if (IsFileLexer())
      33           269990:     return IncludeMacroStack.empty();
      34                 : 
      35                 :   // If there are any stacked lexers, we're in a #include.
      36                 :   assert(IsFileLexer(IncludeMacroStack[0]) &&
                        1: branch 2 taken
                        0: branch 3 not taken
      37                1:          "Top level include stack isn't our primary lexer?");
                        0: branch 1 not taken
                        1: branch 2 taken
      38                1:   for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i)
                        0: branch 2 not taken
                        0: branch 3 not taken
      39                0:     if (IsFileLexer(IncludeMacroStack[i]))
      40                0:       return false;
      41                1:   return true;
      42                 : }
      43                 : 
      44                 : /// getCurrentLexer - Return the current file lexer being lexed from.  Note
      45                 : /// that this ignores any potentially active macro expansions and _Pragma
      46                 : /// expansions going on at the time.
      47              685: PreprocessorLexer *Preprocessor::getCurrentFileLexer() const {
                      682: branch 1 taken
                        3: branch 2 taken
      48              685:   if (IsFileLexer())
      49              682:     return CurPPLexer;
      50                 : 
      51                 :   // Look for a stacked lexer.
                        5: branch 1 taken
                        0: branch 2 not taken
      52                5:   for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
      53                5:     const IncludeStackInfo& ISI = IncludeMacroStack[i-1];
                        3: branch 1 taken
                        2: branch 2 taken
      54                5:     if (IsFileLexer(ISI))
      55                3:       return ISI.ThePPLexer;
      56                 :   }
      57                0:   return 0;
      58                 : }
      59                 : 
      60                 : 
      61                 : //===----------------------------------------------------------------------===//
      62                 : // Methods for Entering and Callbacks for leaving various contexts
      63                 : //===----------------------------------------------------------------------===//
      64                 : 
      65                 : /// EnterSourceFile - Add a source file to the top of the include stack and
      66                 : /// start lexing tokens from it instead of the current buffer.
      67                 : bool Preprocessor::EnterSourceFile(FileID FID, const DirectoryLookup *CurDir,
      68             5606:                                    std::string &ErrorStr) {
                     5606: branch 1 taken
                        0: branch 2 not taken
      69             5606:   assert(CurTokenLexer == 0 && "Cannot #include a file inside a macro!");
      70             5606:   ++NumEnteredSourceFiles;
      71                 : 
                      160: branch 1 taken
                     5446: branch 2 taken
      72             5606:   if (MaxIncludeStackDepth < IncludeMacroStack.size())
      73              160:     MaxIncludeStackDepth = IncludeMacroStack.size();
      74                 : 
                        5: branch 1 taken
                     5601: branch 2 taken
      75             5606:   if (PTH) {
                        4: branch 2 taken
                        1: branch 3 taken
      76                5:     if (PTHLexer *PL = PTH->CreateLexer(FID)) {
      77                4:       EnterSourceFileWithPTH(PL, CurDir);
      78                4:       return false;
      79                 :     }
      80                 :   }
      81                 :   
      82                 :   // Get the MemoryBuffer for this FID, if it fails, we fail.
      83                 :   const llvm::MemoryBuffer *InputFile =
      84             5602:     getSourceManager().getBuffer(FID, &ErrorStr);
                        0: branch 1 not taken
                     5602: branch 2 taken
      85             5602:   if (!ErrorStr.empty())
      86                0:     return true;
      87                 :   
      88             5602:   EnterSourceFileWithLexer(new Lexer(FID, InputFile, *this), CurDir);
      89             5602:   return false;
      90                 : }
      91                 : 
      92                 : /// EnterSourceFileWithLexer - Add a source file to the top of the include stack
      93                 : ///  and start lexing tokens from it instead of the current buffer.
      94                 : void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
      95             5618:                                             const DirectoryLookup *CurDir) {
      96                 : 
      97                 :   // Add the current lexer to the include stack.
                     2523: branch 0 taken
                     3095: branch 1 taken
                        5: branch 3 taken
                     2518: branch 4 taken
                     3100: branch 5 taken
                     2518: branch 6 taken
      98             5618:   if (CurPPLexer || CurTokenLexer)
      99             3100:     PushIncludeMacroStack();
     100                 : 
     101             5618:   CurLexer.reset(TheLexer);
     102             5618:   CurPPLexer = TheLexer;
     103             5618:   CurDirLookup = CurDir;
     104                 : 
     105                 :   // Notify the client, if desired, that we are in a new source file.
                      480: branch 0 taken
                     5138: branch 1 taken
                      472: branch 3 taken
                        8: branch 4 taken
                      472: branch 5 taken
                     5146: branch 6 taken
     106             5618:   if (Callbacks && !CurLexer->Is_PragmaLexer) {
     107                 :     SrcMgr::CharacteristicKind FileType =
     108              472:        SourceMgr.getFileCharacteristic(CurLexer->getFileLoc());
     109                 : 
     110                 :     Callbacks->FileChanged(CurLexer->getFileLoc(),
     111              472:                            PPCallbacks::EnterFile, FileType);
     112                 :   }
     113             5618: }
     114                 : 
     115                 : /// EnterSourceFileWithPTH - Add a source file to the top of the include stack
     116                 : /// and start getting tokens from it using the PTH cache.
     117                 : void Preprocessor::EnterSourceFileWithPTH(PTHLexer *PL,
     118                4:                                           const DirectoryLookup *CurDir) {
     119                 : 
                        1: branch 0 taken
                        3: branch 1 taken
                        0: branch 3 not taken
                        1: branch 4 taken
                        3: branch 5 taken
                        1: branch 6 taken
     120                4:   if (CurPPLexer || CurTokenLexer)
     121                3:     PushIncludeMacroStack();
     122                 : 
     123                4:   CurDirLookup = CurDir;
     124                4:   CurPTHLexer.reset(PL);
     125                4:   CurPPLexer = CurPTHLexer.get();
     126                 : 
     127                 :   // Notify the client, if desired, that we are in a new source file.
                        4: branch 0 taken
                        0: branch 1 not taken
     128                4:   if (Callbacks) {
     129                4:     FileID FID = CurPPLexer->getFileID();
     130                4:     SourceLocation EnterLoc = SourceMgr.getLocForStartOfFile(FID);
     131                 :     SrcMgr::CharacteristicKind FileType =
     132                4:       SourceMgr.getFileCharacteristic(EnterLoc);
     133                4:     Callbacks->FileChanged(EnterLoc, PPCallbacks::EnterFile, FileType);
     134                 :   }
     135                4: }
     136                 : 
     137                 : /// EnterMacro - Add a Macro to the top of the include stack and start lexing
     138                 : /// tokens from it instead of the current buffer.
     139                 : void Preprocessor::EnterMacro(Token &Tok, SourceLocation ILEnd,
     140             7515:                               MacroArgs *Args) {
     141             7515:   PushIncludeMacroStack();
     142             7515:   CurDirLookup = 0;
     143                 : 
                      407: branch 0 taken
                     7108: branch 1 taken
     144             7515:   if (NumCachedTokenLexers == 0) {
     145              407:     CurTokenLexer.reset(new TokenLexer(Tok, ILEnd, Args, *this));
     146                 :   } else {
     147             7108:     CurTokenLexer.reset(TokenLexerCache[--NumCachedTokenLexers]);
     148             7108:     CurTokenLexer->Init(Tok, ILEnd, Args);
     149                 :   }
     150             7515: }
     151                 : 
     152                 : /// EnterTokenStream - Add a "macro" context to the top of the include stack,
     153                 : /// which will cause the lexer to start returning the specified tokens.
     154                 : ///
     155                 : /// If DisableMacroExpansion is true, tokens lexed from the token stream will
     156                 : /// not be subject to further macro expansion.  Otherwise, these tokens will
     157                 : /// be re-macro-expanded when/if expansion is enabled.
     158                 : ///
     159                 : /// If OwnsTokens is false, this method assumes that the specified stream of
     160                 : /// tokens has a permanent owner somewhere, so they do not need to be copied.
     161                 : /// If it is true, it assumes the array of tokens is allocated with new[] and
     162                 : /// must be freed.
     163                 : ///
     164                 : void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks,
     165                 :                                     bool DisableMacroExpansion,
     166             1753:                                     bool OwnsTokens) {
     167                 :   // Save our current state.
     168             1753:   PushIncludeMacroStack();
     169             1753:   CurDirLookup = 0;
     170                 : 
     171                 :   // Create a macro expander to expand from the specified token stream.
                      545: branch 0 taken
                     1208: branch 1 taken
     172             1753:   if (NumCachedTokenLexers == 0) {
     173                 :     CurTokenLexer.reset(new TokenLexer(Toks, NumToks, DisableMacroExpansion,
     174              545:                                        OwnsTokens, *this));
     175                 :   } else {
     176             1208:     CurTokenLexer.reset(TokenLexerCache[--NumCachedTokenLexers]);
     177             1208:     CurTokenLexer->Init(Toks, NumToks, DisableMacroExpansion, OwnsTokens);
     178                 :   }
     179             1753: }
     180                 : 
     181                 : /// HandleEndOfFile - This callback is invoked when the lexer hits the end of
     182                 : /// the current file.  This either returns the EOF token or pops a level off
     183                 : /// the include stack and keeps going.
     184            14311: bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
     185                 :   assert(!CurTokenLexer &&
                    14311: branch 1 taken
                        0: branch 2 not taken
     186            14311:          "Ending a file when currently in a macro!");
     187                 : 
     188                 :   // See if this file had a controlling macro.
                     5618: branch 0 taken
                     8693: branch 1 taken
     189            14311:   if (CurPPLexer) {  // Not ending a macro, ignore it.
                      283: branch 0 taken
                     5335: branch 1 taken
     190             5618:     if (const IdentifierInfo *ControllingMacro =
     191             5618:           CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
     192                 :       // Okay, this has a controlling macro, remember in HeaderFileInfo.
                      283: branch 0 taken
                        0: branch 1 not taken
     193              283:       if (const FileEntry *FE =
     194              283:             SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))
     195              283:         HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
     196                 :     }
     197                 :   }
     198                 : 
     199                 :   // If this is a #include'd file, pop it off the include stack and continue
     200                 :   // lexing the #includer file.
                    11796: branch 1 taken
                     2515: branch 2 taken
     201            14311:   if (!IncludeMacroStack.empty()) {
     202                 :     // We're done with the #included file.
     203            11796:     RemoveTopOfLexerStack();
     204                 : 
     205                 :     // Notify the client, if desired, that we are in a new source file.
                     4197: branch 0 taken
                     7599: branch 1 taken
                      282: branch 2 taken
                     3915: branch 3 taken
                      279: branch 4 taken
                        3: branch 5 taken
     206            11796:     if (Callbacks && !isEndOfMacro && CurPPLexer) {
     207                 :       SrcMgr::CharacteristicKind FileType =
     208              279:         SourceMgr.getFileCharacteristic(CurPPLexer->getSourceLocation());
     209                 :       Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
     210              279:                              PPCallbacks::ExitFile, FileType);
     211                 :     }
     212                 : 
     213                 :     // Client should lex another token.
     214            11796:     return false;
     215                 :   }
     216                 : 
     217                 :   // If the file ends with a newline, form the EOF token on the newline itself,
     218                 :   // rather than "on the line following it", which doesn't exist.  This makes
     219                 :   // diagnostics relating to the end of file include the last file that the user
     220                 :   // actually typed, which is goodness.
                     2514: branch 1 taken
                        1: branch 2 taken
     221             2515:   if (CurLexer) {
     222             2514:     const char *EndPos = CurLexer->BufferEnd;
                     2470: branch 1 taken
                       44: branch 2 taken
                       86: branch 3 taken
                     2384: branch 4 taken
                        0: branch 5 not taken
                       86: branch 6 taken
                     2384: branch 7 taken
                      130: branch 8 taken
     223             2514:     if (EndPos != CurLexer->BufferStart &&
     224                 :         (EndPos[-1] == '\n' || EndPos[-1] == '\r')) {
     225             2384:       --EndPos;
     226                 : 
     227                 :       // Handle \n\r and \r\n:
                     2383: branch 1 taken
                        1: branch 2 taken
                     1727: branch 3 taken
                      656: branch 4 taken
                        1: branch 5 taken
                     1726: branch 6 taken
                        1: branch 7 taken
                      656: branch 8 taken
                        1: branch 9 taken
                     2383: branch 10 taken
     228             2384:       if (EndPos != CurLexer->BufferStart &&
     229                 :           (EndPos[-1] == '\n' || EndPos[-1] == '\r') &&
     230                 :           EndPos[-1] != EndPos[0])
     231                1:         --EndPos;
     232                 :     }
     233                 : 
     234             2514:     Result.startToken();
     235             2514:     CurLexer->BufferPtr = EndPos;
     236             2514:     CurLexer->FormTokenWithChars(Result, EndPos, tok::eof);
     237                 : 
     238                 :     // We're done with the #included file.
     239             2514:     CurLexer.reset();
     240                 :   } else {
                        1: branch 1 taken
                        0: branch 2 not taken
     241                1:     assert(CurPTHLexer && "Got EOF but no current lexer set!");
     242                1:     CurPTHLexer->getEOF(Result);
     243                1:     CurPTHLexer.reset();
     244                 :   }
     245                 : 
     246             2515:   CurPPLexer = 0;
     247                 : 
     248                 :   // This is the end of the top-level file.  If the diag::pp_macro_not_used
     249                 :   // diagnostic is enabled, look for macros that have not been used.
                        0: branch 2 not taken
                     2515: branch 3 taken
     250             2515:   if (getDiagnostics().getDiagnosticLevel(diag::pp_macro_not_used) !=
     251                 :         Diagnostic::Ignored) {
                        0: branch 4 not taken
                        0: branch 5 not taken
     252                0:     for (macro_iterator I = macro_begin(false), E = macro_end(false); 
     253                 :          I != E; ++I)
                        0: branch 2 not taken
                        0: branch 3 not taken
     254                0:       if (!I->second->isUsed())
     255                0:         Diag(I->second->getDefinitionLoc(), diag::pp_macro_not_used);
     256                 :   }
     257             2515:   return true;
     258                 : }
     259                 : 
     260                 : /// HandleEndOfTokenLexer - This callback is invoked when the current TokenLexer
     261                 : /// hits the end of its token stream.
     262             8693: bool Preprocessor::HandleEndOfTokenLexer(Token &Result) {
     263                 :   assert(CurTokenLexer && !CurPPLexer &&
                     8693: branch 1 taken
                        0: branch 2 not taken
                     8693: branch 3 taken
                        0: branch 4 not taken
     264             8693:          "Ending a macro when currently in a #include file!");
     265                 : 
     266                 :   // Delete or cache the now-dead macro expander.
                       13: branch 0 taken
                     8680: branch 1 taken
     267             8693:   if (NumCachedTokenLexers == TokenLexerCacheSize)
     268               13:     CurTokenLexer.reset();
     269                 :   else
     270             8680:     TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.take();
     271                 : 
     272                 :   // Handle this like a #include file being popped off the stack.
     273             8693:   return HandleEndOfFile(Result, true);
     274                 : }
     275                 : 
     276                 : /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
     277                 : /// lexer stack.  This should only be used in situations where the current
     278                 : /// state of the top-of-stack lexer is unknown.
     279            84296: void Preprocessor::RemoveTopOfLexerStack() {
                    84296: branch 1 taken
                        0: branch 2 not taken
     280            84296:   assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load");
     281                 : 
                      567: branch 1 taken
                    83729: branch 2 taken
     282            84296:   if (CurTokenLexer) {
     283                 :     // Delete or cache the now-dead macro expander.
                        0: branch 0 not taken
                      567: branch 1 taken
     284              567:     if (NumCachedTokenLexers == TokenLexerCacheSize)
     285                0:       CurTokenLexer.reset();
     286                 :     else
     287              567:       TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.take();
     288                 :   }
     289                 : 
     290            84296:   PopIncludeMacroStack();
     291            84296: }
     292                 : 
     293                 : /// HandleMicrosoftCommentPaste - When the macro expander pastes together a
     294                 : /// comment (/##/) in microsoft mode, this method handles updating the current
     295                 : /// state, returning the token on the next source line.
     296                2: void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) {
     297                 :   assert(CurTokenLexer && !CurPPLexer &&
                        2: branch 1 taken
                        0: branch 2 not taken
                        2: branch 3 taken
                        0: branch 4 not taken
     298                2:          "Pasted comment can only be formed from macro");
     299                 : 
     300                 :   // We handle this by scanning for the closest real lexer, switching it to
     301                 :   // raw mode and preprocessor mode.  This will cause it to return \n as an
     302                 :   // explicit EOM token.
     303                2:   PreprocessorLexer *FoundLexer = 0;
     304                2:   bool LexerWasInPPMode = false;
                        3: branch 1 taken
                        0: branch 2 not taken
     305                3:   for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
     306                3:     IncludeStackInfo &ISI = *(IncludeMacroStack.end()-i-1);
                        2: branch 0 taken
                        1: branch 1 taken
     307                3:     if (ISI.ThePPLexer == 0) continue;  // Scan for a real lexer.
     308                 : 
     309                 :     // Once we find a real lexer, mark it as raw mode (disabling macro
     310                 :     // expansions) and preprocessor mode (return EOM).  We know that the lexer
     311                 :     // was *not* in raw mode before, because the macro that the comment came
     312                 :     // from was expanded.  However, it could have already been in preprocessor
     313                 :     // mode (#if COMMENT) in which case we have to return it to that mode and
     314                 :     // return EOM.
     315                2:     FoundLexer = ISI.ThePPLexer;
     316                2:     FoundLexer->LexingRawMode = true;
     317                2:     LexerWasInPPMode = FoundLexer->ParsingPreprocessorDirective;
     318                2:     FoundLexer->ParsingPreprocessorDirective = true;
     319                2:     break;
     320                 :   }
     321                 : 
     322                 :   // Okay, we either found and switched over the lexer, or we didn't find a
     323                 :   // lexer.  In either case, finish off the macro the comment came from, getting
     324                 :   // the next token.
                        2: branch 1 taken
                        0: branch 2 not taken
     325                2:   if (!HandleEndOfTokenLexer(Tok)) Lex(Tok);
     326                 : 
     327                 :   // Discarding comments as long as we don't have EOF or EOM.  This 'comments
     328                 :   // out' the rest of the line, including any tokens that came from other macros
     329                 :   // that were active, as in:
     330                 :   //  #define submacro a COMMENT b
     331                 :   //    submacro c
     332                 :   // which should lex to 'a' only: 'b' and 'c' should be removed.
                       13: branch 1 taken
                        2: branch 2 taken
                       13: branch 4 taken
                        0: branch 5 not taken
                       13: branch 6 taken
                        2: branch 7 taken
     333               17:   while (Tok.isNot(tok::eom) && Tok.isNot(tok::eof))
     334               13:     Lex(Tok);
     335                 : 
     336                 :   // If we got an eom token, then we successfully found the end of the line.
                        2: branch 1 taken
                        0: branch 2 not taken
     337                2:   if (Tok.is(tok::eom)) {
                        0: branch 0 not taken
                        2: branch 1 taken
     338                2:     assert(FoundLexer && "Can't get end of line without an active lexer");
     339                 :     // Restore the lexer back to normal mode instead of raw mode.
     340                2:     FoundLexer->LexingRawMode = false;
     341                 : 
     342                 :     // If the lexer was already in preprocessor mode, just return the EOM token
     343                 :     // to finish the preprocessor line.
                        2: branch 0 taken
                        0: branch 1 not taken
     344                2:     if (LexerWasInPPMode) return;
     345                 : 
     346                 :     // Otherwise, switch out of PP mode and return the next lexed token.
     347                2:     FoundLexer->ParsingPreprocessorDirective = false;
     348                2:     return Lex(Tok);
     349                 :   }
     350                 : 
     351                 :   // If we got an EOF token, then we reached the end of the token stream but
     352                 :   // didn't find an explicit \n.  This can only happen if there was no lexer
     353                 :   // active (an active lexer would return EOM at EOF if there was no \n in
     354                 :   // preprocessor directive mode), so just return EOF as our token.
                        0: branch 0 not taken
                        0: branch 1 not taken
     355                0:   assert(!FoundLexer && "Lexer should return EOM before EOF in PP mode");
     356                 : }

Generated: 2010-02-10 01:31 by zcov