diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Lex/PPDirectives.cpp | 44 | ||||
-rw-r--r-- | lib/Lex/PPMacroExpansion.cpp | 5 | ||||
-rw-r--r-- | lib/Lex/PreprocessingRecord.cpp | 37 | ||||
-rw-r--r-- | lib/Serialization/ASTReader.cpp | 68 | ||||
-rw-r--r-- | lib/Serialization/ASTWriter.cpp | 33 |
5 files changed, 154 insertions, 33 deletions
diff --git a/lib/Lex/PPDirectives.cpp b/lib/Lex/PPDirectives.cpp index 8f66d99b18..d5abc20a3d 100644 --- a/lib/Lex/PPDirectives.cpp +++ b/lib/Lex/PPDirectives.cpp @@ -591,9 +591,11 @@ TryAgain: // C99 6.10.2 - Source File Inclusion. case tok::pp_include: - return HandleIncludeDirective(Result); // Handle #include. + // Handle #include. + return HandleIncludeDirective(SavedHash.getLocation(), Result); case tok::pp___include_macros: - return HandleIncludeMacrosDirective(Result); // Handle -imacros. + // Handle -imacros. + return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result); // C99 6.10.3 - Macro Replacement. case tok::pp_define: @@ -615,9 +617,9 @@ TryAgain: // GNU Extensions. case tok::pp_import: - return HandleImportDirective(Result); + return HandleImportDirective(SavedHash.getLocation(), Result); case tok::pp_include_next: - return HandleIncludeNextDirective(Result); + return HandleIncludeNextDirective(SavedHash.getLocation(), Result); case tok::pp_warning: Diag(Result, diag::ext_pp_warning_directive); @@ -1034,11 +1036,14 @@ bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc, /// false if the > was found, otherwise it returns true if it finds and consumes /// the EOM marker. bool Preprocessor::ConcatenateIncludeName( - llvm::SmallString<128> &FilenameBuffer) { + llvm::SmallString<128> &FilenameBuffer, + SourceLocation &End) { Token CurTok; Lex(CurTok); while (CurTok.isNot(tok::eom)) { + End = CurTok.getLocation(); + // Append the spelling of this token to the buffer. If there was a space // before it, add it now. if (CurTok.hasLeadingSpace()) @@ -1077,7 +1082,8 @@ bool Preprocessor::ConcatenateIncludeName( /// routine with functionality shared between #include, #include_next and /// #import. LookupFrom is set when this is a #include_next directive, it /// specifies the file to start searching from. -void Preprocessor::HandleIncludeDirective(Token &IncludeTok, +void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc, + Token &IncludeTok, const DirectoryLookup *LookupFrom, bool isImport) { @@ -1087,7 +1093,8 @@ void Preprocessor::HandleIncludeDirective(Token &IncludeTok, // Reserve a buffer to get the spelling. llvm::SmallString<128> FilenameBuffer; llvm::StringRef Filename; - + SourceLocation End; + switch (FilenameTok.getKind()) { case tok::eom: // If the token kind is EOM, the error has already been diagnosed. @@ -1096,13 +1103,14 @@ void Preprocessor::HandleIncludeDirective(Token &IncludeTok, case tok::angle_string_literal: case tok::string_literal: Filename = getSpelling(FilenameTok, FilenameBuffer); + End = FilenameTok.getLocation(); break; case tok::less: // This could be a <foo/bar.h> file coming from a macro expansion. In this // case, glue the tokens together into FilenameBuffer and interpret those. FilenameBuffer.push_back('<'); - if (ConcatenateIncludeName(FilenameBuffer)) + if (ConcatenateIncludeName(FilenameBuffer, End)) return; // Found <eom> but no ">"? Diagnostic already emitted. Filename = FilenameBuffer.str(); break; @@ -1141,6 +1149,11 @@ void Preprocessor::HandleIncludeDirective(Token &IncludeTok, return; } + // Notify the callback object that we've seen an inclusion directive. + if (Callbacks) + Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled, File, + End); + // The #included file will be considered to be a system header if either it is // in a system include directory, or if the #includer is a system include // header. @@ -1170,7 +1183,8 @@ void Preprocessor::HandleIncludeDirective(Token &IncludeTok, /// HandleIncludeNextDirective - Implements #include_next. /// -void Preprocessor::HandleIncludeNextDirective(Token &IncludeNextTok) { +void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc, + Token &IncludeNextTok) { Diag(IncludeNextTok, diag::ext_pp_include_next_directive); // #include_next is like #include, except that we start searching after @@ -1187,23 +1201,25 @@ void Preprocessor::HandleIncludeNextDirective(Token &IncludeNextTok) { ++Lookup; } - return HandleIncludeDirective(IncludeNextTok, Lookup); + return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup); } /// HandleImportDirective - Implements #import. /// -void Preprocessor::HandleImportDirective(Token &ImportTok) { +void Preprocessor::HandleImportDirective(SourceLocation HashLoc, + Token &ImportTok) { if (!Features.ObjC1) // #import is standard for ObjC. Diag(ImportTok, diag::ext_pp_import_directive); - return HandleIncludeDirective(ImportTok, 0, true); + return HandleIncludeDirective(HashLoc, ImportTok, 0, true); } /// HandleIncludeMacrosDirective - The -imacros command line option turns into a /// pseudo directive in the predefines buffer. This handles it by sucking all /// tokens through the preprocessor and discarding them (only keeping the side /// effects on the preprocessor). -void Preprocessor::HandleIncludeMacrosDirective(Token &IncludeMacrosTok) { +void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc, + Token &IncludeMacrosTok) { // This directive should only occur in the predefines buffer. If not, emit an // error and reject it. SourceLocation Loc = IncludeMacrosTok.getLocation(); @@ -1216,7 +1232,7 @@ void Preprocessor::HandleIncludeMacrosDirective(Token &IncludeMacrosTok) { // Treat this as a normal #include for checking purposes. If this is // successful, it will push a new lexer onto the include stack. - HandleIncludeDirective(IncludeMacrosTok, 0, false); + HandleIncludeDirective(HashLoc, IncludeMacrosTok, 0, false); Token TmpTok; do { diff --git a/lib/Lex/PPMacroExpansion.cpp b/lib/Lex/PPMacroExpansion.cpp index 19a6ca8521..68fc56f7b7 100644 --- a/lib/Lex/PPMacroExpansion.cpp +++ b/lib/Lex/PPMacroExpansion.cpp @@ -570,7 +570,8 @@ static bool EvaluateHasIncludeCommon(bool &Result, Token &Tok, // Reserve a buffer to get the spelling. llvm::SmallString<128> FilenameBuffer; llvm::StringRef Filename; - + SourceLocation EndLoc; + switch (Tok.getKind()) { case tok::eom: // If the token kind is EOM, the error has already been diagnosed. @@ -589,7 +590,7 @@ static bool EvaluateHasIncludeCommon(bool &Result, Token &Tok, // This could be a <foo/bar.h> file coming from a macro expansion. In this // case, glue the tokens together into FilenameBuffer and interpret those. FilenameBuffer.push_back('<'); - if (PP.ConcatenateIncludeName(FilenameBuffer)) + if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc)) return false; // Found <eom> but no ">"? Diagnostic already emitted. Filename = FilenameBuffer.str(); break; diff --git a/lib/Lex/PreprocessingRecord.cpp b/lib/Lex/PreprocessingRecord.cpp index c446d96b45..34421779c9 100644 --- a/lib/Lex/PreprocessingRecord.cpp +++ b/lib/Lex/PreprocessingRecord.cpp @@ -14,6 +14,8 @@ #include "clang/Lex/PreprocessingRecord.h" #include "clang/Lex/MacroInfo.h" #include "clang/Lex/Token.h" +#include "clang/Basic/IdentifierTable.h" +#include "llvm/Support/ErrorHandling.h" using namespace clang; @@ -127,3 +129,38 @@ void PreprocessingRecord::MacroUndefined(SourceLocation Loc, MacroDefinitions.erase(Pos); } +void PreprocessingRecord::InclusionDirective(SourceLocation HashLoc, + const clang::Token &IncludeTok, + llvm::StringRef FileName, + bool IsAngled, + const FileEntry *File, + clang::SourceLocation EndLoc) { + InclusionDirective::InclusionKind Kind = InclusionDirective::Include; + + switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) { + case tok::pp_include: + Kind = InclusionDirective::Include; + break; + + case tok::pp_import: + Kind = InclusionDirective::Import; + break; + + case tok::pp_include_next: + Kind = InclusionDirective::IncludeNext; + break; + + case tok::pp___include_macros: + Kind = InclusionDirective::IncludeMacros; + break; + + default: + llvm_unreachable("Unknown include directive kind"); + return; + } + + clang::InclusionDirective *ID + = new (*this) clang::InclusionDirective(Kind, FileName, !IsAngled, File, + SourceRange(HashLoc, EndLoc)); + PreprocessedEntities.push_back(ID); +} diff --git a/lib/Serialization/ASTReader.cpp b/lib/Serialization/ASTReader.cpp index d9e97fe8b3..5389b51944 100644 --- a/lib/Serialization/ASTReader.cpp +++ b/lib/Serialization/ASTReader.cpp @@ -1337,18 +1337,21 @@ bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, } while (true) { + uint64_t Offset = Cursor.GetCurrentBitNo(); unsigned Code = Cursor.ReadCode(); - + // We expect all abbrevs to be at the start of the block. - if (Code != llvm::bitc::DEFINE_ABBREV) + if (Code != llvm::bitc::DEFINE_ABBREV) { + Cursor.JumpToBit(Offset); return false; + } Cursor.ReadAbbrevRecord(); } } void ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) { assert(PP && "Forgot to set Preprocessor ?"); - llvm::BitstreamCursor &Stream = F.Stream; + llvm::BitstreamCursor &Stream = F.MacroCursor; // Keep track of where we are in the stream, then jump back there // after reading this macro. @@ -1381,9 +1384,12 @@ void ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) { } // Read a record. + const char *BlobStart = 0; + unsigned BlobLen = 0; Record.clear(); PreprocessorRecordTypes RecType = - (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record); + (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart, + BlobLen); switch (RecType) { case PP_MACRO_OBJECT_LIKE: case PP_MACRO_FUNCTION_LIKE: { @@ -1524,6 +1530,41 @@ void ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) { return; } + + case PP_INCLUSION_DIRECTIVE: { + // If we already have a macro, that means that we've hit the end + // of the definition of the macro we were looking for. We're + // done. + if (Macro) + return; + + if (!PP->getPreprocessingRecord()) { + Error("missing preprocessing record in AST file"); + return; + } + + PreprocessingRecord &PPRec = *PP->getPreprocessingRecord(); + if (PPRec.getPreprocessedEntity(Record[0])) + return; + + const char *FullFileNameStart = BlobStart + Record[3]; + const FileEntry *File + = PP->getFileManager().getFile(FullFileNameStart, + FullFileNameStart + (BlobLen - Record[3])); + + // FIXME: Stable encoding + InclusionDirective::InclusionKind Kind + = static_cast<InclusionDirective::InclusionKind>(Record[5]); + InclusionDirective *ID + = new (PPRec) InclusionDirective(Kind, + llvm::StringRef(BlobStart, Record[3]), + Record[4], + File, + SourceRange(ReadSourceLocation(F, Record[1]), + ReadSourceLocation(F, Record[2]))); + PPRec.SetPreallocatedEntity(Record[0], ID); + return; + } } } } @@ -1538,22 +1579,14 @@ void ASTReader::ReadDefinedMacros() { continue; llvm::BitstreamCursor Cursor = MacroCursor; - if (Cursor.EnterSubBlock(PREPROCESSOR_BLOCK_ID)) { - Error("malformed preprocessor block record in AST file"); - return; - } - + Cursor.JumpToBit(F.MacroStartOffset); + RecordData Record; while (true) { uint64_t Offset = Cursor.GetCurrentBitNo(); unsigned Code = Cursor.ReadCode(); - if (Code == llvm::bitc::END_BLOCK) { - if (Cursor.ReadBlockEnd()) { - Error("error at end of preprocessor block in AST file"); - return; - } + if (Code == llvm::bitc::END_BLOCK) break; - } if (Code == llvm::bitc::ENTER_SUBBLOCK) { // No known subblocks, always skip them. @@ -1589,6 +1622,7 @@ void ASTReader::ReadDefinedMacros() { case PP_MACRO_INSTANTIATION: case PP_MACRO_DEFINITION: + case PP_INCLUSION_DIRECTIVE: // Read the macro record. // FIXME: That's a stupid way to do this. We should reuse this cursor. ReadMacroRecord(F, Offset); @@ -1686,10 +1720,12 @@ ASTReader::ReadASTBlock(PerFileData &F) { if (PP) PP->setExternalSource(this); - if (Stream.SkipBlock()) { + if (Stream.SkipBlock() || + ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) { Error("malformed block record in AST file"); return Failure; } + F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo(); break; case SOURCE_MANAGER_BLOCK_ID: diff --git a/lib/Serialization/ASTWriter.cpp b/lib/Serialization/ASTWriter.cpp index 2cbe08190b..8cf7e85dc6 100644 --- a/lib/Serialization/ASTWriter.cpp +++ b/lib/Serialization/ASTWriter.cpp @@ -1256,16 +1256,32 @@ void ASTWriter::WritePreprocessor(const Preprocessor &PP) { } // Enter the preprocessor block. - Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 2); + Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3); // If the AST file contains __DATE__ or __TIME__ emit a warning about this. // FIXME: use diagnostics subsystem for localization etc. if (PP.SawDateOrTime()) fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n"); + // Loop over all the macro definitions that are live at the end of the file, // emitting each to the PP section. PreprocessingRecord *PPRec = PP.getPreprocessingRecord(); + unsigned InclusionAbbrev = 0; + if (PPRec) { + using namespace llvm; + BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); + Abbrev->Add(BitCodeAbbrevOp(PP_INCLUSION_DIRECTIVE)); + Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index + Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location + Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location + Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length + Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes + Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind + Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); + InclusionAbbrev = Stream.EmitAbbrev(Abbrev); + } + for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end(); I != E; ++I) { // FIXME: This emits macros in hash table order, we should do it in a stable @@ -1381,6 +1397,21 @@ void ASTWriter::WritePreprocessor(const Preprocessor &PP) { Stream.EmitRecord(PP_MACRO_DEFINITION, Record); continue; } + + if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) { + Record.push_back(PP_INCLUSION_DIRECTIVE); + Record.push_back(IndexBase + NumPreprocessingRecords++); + AddSourceLocation(ID->getSourceRange().getBegin(), Record); + AddSourceLocation(ID->getSourceRange().getEnd(), Record); + Record.push_back(ID->getFileName().size()); + Record.push_back(ID->wasInQuotes()); + Record.push_back(static_cast<unsigned>(ID->getKind())); + llvm::SmallString<64> Buffer; + Buffer += ID->getFileName(); + Buffer += ID->getFile()->getName(); + Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer); + continue; + } } } |