1 //===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===//
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 Preprocessor interface.
11 //
12 //===----------------------------------------------------------------------===//
13 //
14 // Options to support:
15 //   -H       - Print the name of each header file used.
16 //   -d[DNI] - Dump various things.
17 //   -fworking-directory - #line's with preprocessor's working dir.
18 //   -fpreprocessed
19 //   -dependency-file,-M,-MM,-MF,-MG,-MP,-MT,-MQ,-MD,-MMD
20 //   -W*
21 //   -w
22 //
23 // Messages to emit:
24 //   "Multiple include guards may be useful for:\n"
25 //
26 //===----------------------------------------------------------------------===//
27 
28 #include "clang/Lex/Preprocessor.h"
29 #include "MacroArgs.h"
30 #include "clang/Lex/ExternalPreprocessorSource.h"
31 #include "clang/Lex/HeaderSearch.h"
32 #include "clang/Lex/MacroInfo.h"
33 #include "clang/Lex/Pragma.h"
34 #include "clang/Lex/PreprocessingRecord.h"
35 #include "clang/Lex/ScratchBuffer.h"
36 #include "clang/Lex/LexDiagnostic.h"
37 #include "clang/Lex/CodeCompletionHandler.h"
38 #include "clang/Basic/SourceManager.h"
39 #include "clang/Basic/FileManager.h"
40 #include "clang/Basic/TargetInfo.h"
41 #include "llvm/ADT/APFloat.h"
42 #include "llvm/ADT/SmallVector.h"
43 #include "llvm/Support/MemoryBuffer.h"
44 #include "llvm/Support/raw_ostream.h"
45 using namespace clang;
46 
47 //===----------------------------------------------------------------------===//
48 ExternalPreprocessorSource::~ExternalPreprocessorSource() { }
49 
50 Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
51                            const TargetInfo &target, SourceManager &SM,
52                            HeaderSearch &Headers,
53                            IdentifierInfoLookup* IILookup,
54                            bool OwnsHeaders)
55   : Diags(&diags), Features(opts), Target(target),FileMgr(Headers.getFileMgr()),
56     SourceMgr(SM),
57     HeaderInfo(Headers), ExternalSource(0),
58     Identifiers(opts, IILookup), BuiltinInfo(Target), CodeComplete(0),
59     CodeCompletionFile(0), SkipMainFilePreamble(0, true), CurPPLexer(0),
60     CurDirLookup(0), Callbacks(0), MacroArgCache(0), Record(0), MIChainHead(0),
61     MICache(0) {
62   ScratchBuf = new ScratchBuffer(SourceMgr);
63   CounterValue = 0; // __COUNTER__ starts at 0.
64   OwnsHeaderSearch = OwnsHeaders;
65 
66   // Clear stats.
67   NumDirectives = NumDefined = NumUndefined = NumPragma = 0;
68   NumIf = NumElse = NumEndif = 0;
69   NumEnteredSourceFiles = 0;
70   NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0;
71   NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0;
72   MaxIncludeStackDepth = 0;
73   NumSkipped = 0;
74 
75   // Default to discarding comments.
76   KeepComments = false;
77   KeepMacroComments = false;
78 
79   // Macro expansion is enabled.
80   DisableMacroExpansion = false;
81   InMacroArgs = false;
82   NumCachedTokenLexers = 0;
83 
84   CachedLexPos = 0;
85 
86   // We haven't read anything from the external source.
87   ReadMacrosFromExternalSource = false;
88 
89   // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
90   // This gets unpoisoned where it is allowed.
91   (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
92 
93   // Initialize the pragma handlers.
94   PragmaHandlers = new PragmaNamespace(llvm::StringRef());
95   RegisterBuiltinPragmas();
96 
97   // Initialize builtin macros like __LINE__ and friends.
98   RegisterBuiltinMacros();
99 }
100 
101 Preprocessor::~Preprocessor() {
102   assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!");
103 
104   while (!IncludeMacroStack.empty()) {
105     delete IncludeMacroStack.back().TheLexer;
106     delete IncludeMacroStack.back().TheTokenLexer;
107     IncludeMacroStack.pop_back();
108   }
109 
110   // Free any macro definitions.
111   for (MacroInfoChain *I = MIChainHead ; I ; I = I->Next)
112     I->MI.Destroy();
113 
114   // Free any cached macro expanders.
115   for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i)
116     delete TokenLexerCache[i];
117 
118   // Free any cached MacroArgs.
119   for (MacroArgs *ArgList = MacroArgCache; ArgList; )
120     ArgList = ArgList->deallocate();
121 
122   // Release pragma information.
123   delete PragmaHandlers;
124 
125   // Delete the scratch buffer info.
126   delete ScratchBuf;
127 
128   // Delete the header search info, if we own it.
129   if (OwnsHeaderSearch)
130     delete &HeaderInfo;
131 
132   delete Callbacks;
133 }
134 
135 void Preprocessor::setPTHManager(PTHManager* pm) {
136   PTH.reset(pm);
137   FileMgr.addStatCache(PTH->createStatCache());
138 }
139 
140 void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const {
141   llvm::errs() << tok::getTokenName(Tok.getKind()) << " '"
142                << getSpelling(Tok) << "'";
143 
144   if (!DumpFlags) return;
145 
146   llvm::errs() << "\t";
147   if (Tok.isAtStartOfLine())
148     llvm::errs() << " [StartOfLine]";
149   if (Tok.hasLeadingSpace())
150     llvm::errs() << " [LeadingSpace]";
151   if (Tok.isExpandDisabled())
152     llvm::errs() << " [ExpandDisabled]";
153   if (Tok.needsCleaning()) {
154     const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
155     llvm::errs() << " [UnClean='" << llvm::StringRef(Start, Tok.getLength())
156                  << "']";
157   }
158 
159   llvm::errs() << "\tLoc=<";
160   DumpLocation(Tok.getLocation());
161   llvm::errs() << ">";
162 }
163 
164 void Preprocessor::DumpLocation(SourceLocation Loc) const {
165   Loc.dump(SourceMgr);
166 }
167 
168 void Preprocessor::DumpMacro(const MacroInfo &MI) const {
169   llvm::errs() << "MACRO: ";
170   for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
171     DumpToken(MI.getReplacementToken(i));
172     llvm::errs() << "  ";
173   }
174   llvm::errs() << "\n";
175 }
176 
177 void Preprocessor::PrintStats() {
178   llvm::errs() << "\n*** Preprocessor Stats:\n";
179   llvm::errs() << NumDirectives << " directives found:\n";
180   llvm::errs() << "  " << NumDefined << " #define.\n";
181   llvm::errs() << "  " << NumUndefined << " #undef.\n";
182   llvm::errs() << "  #include/#include_next/#import:\n";
183   llvm::errs() << "    " << NumEnteredSourceFiles << " source files entered.\n";
184   llvm::errs() << "    " << MaxIncludeStackDepth << " max include stack depth\n";
185   llvm::errs() << "  " << NumIf << " #if/#ifndef/#ifdef.\n";
186   llvm::errs() << "  " << NumElse << " #else/#elif.\n";
187   llvm::errs() << "  " << NumEndif << " #endif.\n";
188   llvm::errs() << "  " << NumPragma << " #pragma.\n";
189   llvm::errs() << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
190 
191   llvm::errs() << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
192              << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
193              << NumFastMacroExpanded << " on the fast path.\n";
194   llvm::errs() << (NumFastTokenPaste+NumTokenPaste)
195              << " token paste (##) operations performed, "
196              << NumFastTokenPaste << " on the fast path.\n";
197 }
198 
199 Preprocessor::macro_iterator
200 Preprocessor::macro_begin(bool IncludeExternalMacros) const {
201   if (IncludeExternalMacros && ExternalSource &&
202       !ReadMacrosFromExternalSource) {
203     ReadMacrosFromExternalSource = true;
204     ExternalSource->ReadDefinedMacros();
205   }
206 
207   return Macros.begin();
208 }
209 
210 Preprocessor::macro_iterator
211 Preprocessor::macro_end(bool IncludeExternalMacros) const {
212   if (IncludeExternalMacros && ExternalSource &&
213       !ReadMacrosFromExternalSource) {
214     ReadMacrosFromExternalSource = true;
215     ExternalSource->ReadDefinedMacros();
216   }
217 
218   return Macros.end();
219 }
220 
221 bool Preprocessor::SetCodeCompletionPoint(const FileEntry *File,
222                                           unsigned TruncateAtLine,
223                                           unsigned TruncateAtColumn) {
224   using llvm::MemoryBuffer;
225 
226   CodeCompletionFile = File;
227 
228   // Okay to clear out the code-completion point by passing NULL.
229   if (!CodeCompletionFile)
230     return false;
231 
232   // Load the actual file's contents.
233   bool Invalid = false;
234   const MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File, &Invalid);
235   if (Invalid)
236     return true;
237 
238   // Find the byte position of the truncation point.
239   const char *Position = Buffer->getBufferStart();
240   for (unsigned Line = 1; Line < TruncateAtLine; ++Line) {
241     for (; *Position; ++Position) {
242       if (*Position != '\r' && *Position != '\n')
243         continue;
244 
245       // Eat \r\n or \n\r as a single line.
246       if ((Position[1] == '\r' || Position[1] == '\n') &&
247           Position[0] != Position[1])
248         ++Position;
249       ++Position;
250       break;
251     }
252   }
253 
254   Position += TruncateAtColumn - 1;
255 
256   // Truncate the buffer.
257   if (Position < Buffer->getBufferEnd()) {
258     llvm::StringRef Data(Buffer->getBufferStart(),
259                          Position-Buffer->getBufferStart());
260     MemoryBuffer *TruncatedBuffer
261       = MemoryBuffer::getMemBufferCopy(Data, Buffer->getBufferIdentifier());
262     SourceMgr.overrideFileContents(File, TruncatedBuffer);
263   }
264 
265   return false;
266 }
267 
268 bool Preprocessor::isCodeCompletionFile(SourceLocation FileLoc) const {
269   return CodeCompletionFile && FileLoc.isFileID() &&
270     SourceMgr.getFileEntryForID(SourceMgr.getFileID(FileLoc))
271       == CodeCompletionFile;
272 }
273 
274 void Preprocessor::CodeCompleteNaturalLanguage() {
275   SetCodeCompletionPoint(0, 0, 0);
276   getDiagnostics().setSuppressAllDiagnostics(true);
277   if (CodeComplete)
278     CodeComplete->CodeCompleteNaturalLanguage();
279 }
280 
281 
282 /// getSpelling - This method is used to get the spelling of a token into a
283 /// SmallVector. Note that the returned StringRef may not point to the
284 /// supplied buffer if a copy can be avoided.
285 llvm::StringRef Preprocessor::getSpelling(const Token &Tok,
286                                           llvm::SmallVectorImpl<char> &Buffer,
287                                           bool *Invalid) const {
288   // NOTE: this has to be checked *before* testing for an IdentifierInfo.
289   if (Tok.isNot(tok::raw_identifier)) {
290     // Try the fast path.
291     if (const IdentifierInfo *II = Tok.getIdentifierInfo())
292       return II->getName();
293   }
294 
295   // Resize the buffer if we need to copy into it.
296   if (Tok.needsCleaning())
297     Buffer.resize(Tok.getLength());
298 
299   const char *Ptr = Buffer.data();
300   unsigned Len = getSpelling(Tok, Ptr, Invalid);
301   return llvm::StringRef(Ptr, Len);
302 }
303 
304 /// CreateString - Plop the specified string into a scratch buffer and return a
305 /// location for it.  If specified, the source location provides a source
306 /// location for the token.
307 void Preprocessor::CreateString(const char *Buf, unsigned Len, Token &Tok,
308                                 SourceLocation InstantiationLoc) {
309   Tok.setLength(Len);
310 
311   const char *DestPtr;
312   SourceLocation Loc = ScratchBuf->getToken(Buf, Len, DestPtr);
313 
314   if (InstantiationLoc.isValid())
315     Loc = SourceMgr.createInstantiationLoc(Loc, InstantiationLoc,
316                                            InstantiationLoc, Len);
317   Tok.setLocation(Loc);
318 
319   // If this is a raw identifier or a literal token, set the pointer data.
320   if (Tok.is(tok::raw_identifier))
321     Tok.setRawIdentifierData(DestPtr);
322   else if (Tok.isLiteral())
323     Tok.setLiteralData(DestPtr);
324 }
325 
326 
327 
328 //===----------------------------------------------------------------------===//
329 // Preprocessor Initialization Methods
330 //===----------------------------------------------------------------------===//
331 
332 
333 /// EnterMainSourceFile - Enter the specified FileID as the main source file,
334 /// which implicitly adds the builtin defines etc.
335 void Preprocessor::EnterMainSourceFile() {
336   // We do not allow the preprocessor to reenter the main file.  Doing so will
337   // cause FileID's to accumulate information from both runs (e.g. #line
338   // information) and predefined macros aren't guaranteed to be set properly.
339   assert(NumEnteredSourceFiles == 0 && "Cannot reenter the main file!");
340   FileID MainFileID = SourceMgr.getMainFileID();
341 
342   // Enter the main file source buffer.
343   EnterSourceFile(MainFileID, 0, SourceLocation());
344 
345   // If we've been asked to skip bytes in the main file (e.g., as part of a
346   // precompiled preamble), do so now.
347   if (SkipMainFilePreamble.first > 0)
348     CurLexer->SkipBytes(SkipMainFilePreamble.first,
349                         SkipMainFilePreamble.second);
350 
351   // Tell the header info that the main file was entered.  If the file is later
352   // #imported, it won't be re-entered.
353   if (const FileEntry *FE = SourceMgr.getFileEntryForID(MainFileID))
354     HeaderInfo.IncrementIncludeCount(FE);
355 
356   // Preprocess Predefines to populate the initial preprocessor state.
357   llvm::MemoryBuffer *SB =
358     llvm::MemoryBuffer::getMemBufferCopy(Predefines, "<built-in>");
359   assert(SB && "Cannot create predefined source buffer");
360   FileID FID = SourceMgr.createFileIDForMemBuffer(SB);
361   assert(!FID.isInvalid() && "Could not create FileID for predefines?");
362 
363   // Start parsing the predefines.
364   EnterSourceFile(FID, 0, SourceLocation());
365 }
366 
367 void Preprocessor::EndSourceFile() {
368   // Notify the client that we reached the end of the source file.
369   if (Callbacks)
370     Callbacks->EndOfMainFile();
371 }
372 
373 //===----------------------------------------------------------------------===//
374 // Lexer Event Handling.
375 //===----------------------------------------------------------------------===//
376 
377 /// LookUpIdentifierInfo - Given a tok::raw_identifier token, look up the
378 /// identifier information for the token and install it into the token,
379 /// updating the token kind accordingly.
380 IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier) const {
381   assert(Identifier.getRawIdentifierData() != 0 && "No raw identifier data!");
382 
383   // Look up this token, see if it is a macro, or if it is a language keyword.
384   IdentifierInfo *II;
385   if (!Identifier.needsCleaning()) {
386     // No cleaning needed, just use the characters from the lexed buffer.
387     II = getIdentifierInfo(llvm::StringRef(Identifier.getRawIdentifierData(),
388                                            Identifier.getLength()));
389   } else {
390     // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
391     llvm::SmallString<64> IdentifierBuffer;
392     llvm::StringRef CleanedStr = getSpelling(Identifier, IdentifierBuffer);
393     II = getIdentifierInfo(CleanedStr);
394   }
395 
396   // Update the token info (identifier info and appropriate token kind).
397   Identifier.setIdentifierInfo(II);
398   Identifier.setKind(II->getTokenID());
399 
400   return II;
401 }
402 
403 
404 /// HandleIdentifier - This callback is invoked when the lexer reads an
405 /// identifier.  This callback looks up the identifier in the map and/or
406 /// potentially macro expands it or turns it into a named token (like 'for').
407 ///
408 /// Note that callers of this method are guarded by checking the
409 /// IdentifierInfo's 'isHandleIdentifierCase' bit.  If this method changes, the
410 /// IdentifierInfo methods that compute these properties will need to change to
411 /// match.
412 void Preprocessor::HandleIdentifier(Token &Identifier) {
413   assert(Identifier.getIdentifierInfo() &&
414          "Can't handle identifiers without identifier info!");
415 
416   IdentifierInfo &II = *Identifier.getIdentifierInfo();
417 
418   // If this identifier was poisoned, and if it was not produced from a macro
419   // expansion, emit an error.
420   if (II.isPoisoned() && CurPPLexer) {
421     if (&II != Ident__VA_ARGS__)   // We warn about __VA_ARGS__ with poisoning.
422       Diag(Identifier, diag::err_pp_used_poisoned_id);
423     else
424       Diag(Identifier, diag::ext_pp_bad_vaargs_use);
425   }
426 
427   // If this is a macro to be expanded, do it.
428   if (MacroInfo *MI = getMacroInfo(&II)) {
429     if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
430       if (MI->isEnabled()) {
431         if (!HandleMacroExpandedIdentifier(Identifier, MI))
432           return;
433       } else {
434         // C99 6.10.3.4p2 says that a disabled macro may never again be
435         // expanded, even if it's in a context where it could be expanded in the
436         // future.
437         Identifier.setFlag(Token::DisableExpand);
438       }
439     }
440   }
441 
442   // C++ 2.11p2: If this is an alternative representation of a C++ operator,
443   // then we act as if it is the actual operator and not the textual
444   // representation of it.
445   if (II.isCPlusPlusOperatorKeyword())
446     Identifier.setIdentifierInfo(0);
447 
448   // If this is an extension token, diagnose its use.
449   // We avoid diagnosing tokens that originate from macro definitions.
450   // FIXME: This warning is disabled in cases where it shouldn't be,
451   // like "#define TY typeof", "TY(1) x".
452   if (II.isExtensionToken() && !DisableMacroExpansion)
453     Diag(Identifier, diag::ext_token_used);
454 }
455 
456 void Preprocessor::AddCommentHandler(CommentHandler *Handler) {
457   assert(Handler && "NULL comment handler");
458   assert(std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler) ==
459          CommentHandlers.end() && "Comment handler already registered");
460   CommentHandlers.push_back(Handler);
461 }
462 
463 void Preprocessor::RemoveCommentHandler(CommentHandler *Handler) {
464   std::vector<CommentHandler *>::iterator Pos
465   = std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler);
466   assert(Pos != CommentHandlers.end() && "Comment handler not registered");
467   CommentHandlers.erase(Pos);
468 }
469 
470 bool Preprocessor::HandleComment(Token &result, SourceRange Comment) {
471   bool AnyPendingTokens = false;
472   for (std::vector<CommentHandler *>::iterator H = CommentHandlers.begin(),
473        HEnd = CommentHandlers.end();
474        H != HEnd; ++H) {
475     if ((*H)->HandleComment(*this, Comment))
476       AnyPendingTokens = true;
477   }
478   if (!AnyPendingTokens || getCommentRetentionState())
479     return false;
480   Lex(result);
481   return true;
482 }
483 
484 CommentHandler::~CommentHandler() { }
485 
486 CodeCompletionHandler::~CodeCompletionHandler() { }
487 
488 void Preprocessor::createPreprocessingRecord() {
489   if (Record)
490     return;
491 
492   Record = new PreprocessingRecord;
493   addPPCallbacks(Record);
494 }
495