Index: clang-tools-extra/trunk/clang-doc/BitcodeReader.h =================================================================== --- clang-tools-extra/trunk/clang-doc/BitcodeReader.h +++ clang-tools-extra/trunk/clang-doc/BitcodeReader.h @@ -0,0 +1,73 @@ +//===-- BitcodeReader.h - ClangDoc Bitcode Reader --------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements a reader for parsing the clang-doc internal +// representation from LLVM bitcode. The reader takes in a stream of bits and +// generates the set of infos that it represents. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEREADER_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEREADER_H + +#include "BitcodeWriter.h" +#include "Representation.h" +#include "clang/AST/AST.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/Bitcode/BitstreamReader.h" + +namespace clang { +namespace doc { + +// Class to read bitstream into an InfoSet collection +class ClangDocBitcodeReader { +public: + ClangDocBitcodeReader(llvm::BitstreamCursor &Stream) : Stream(Stream) {} + + // Main entry point, calls readBlock to read each block in the given stream. + std::vector> readBitcode(); + +private: + enum class Cursor { BadBlock = 1, Record, BlockEnd, BlockBegin }; + + // Top level parsing + bool validateStream(); + bool readVersion(); + bool readBlockInfoBlock(); + + // Read a block of records into a single Info struct, calls readRecord on each + // record found. + template bool readBlock(unsigned ID, T I); + + // Step through a block of records to find the next data field. + template bool readSubBlock(unsigned ID, T I); + + // Read record data into the given Info data field, calling the appropriate + // parseRecord functions to parse and store the data. + template bool readRecord(unsigned ID, T I); + + // Allocate the relevant type of info and add read data to the object. + template std::unique_ptr createInfo(unsigned ID); + + // Helper function to step through blocks to find and dispatch the next record + // or block to be read. + Cursor skipUntilRecordOrBlock(unsigned &BlockOrRecordID); + + // Helper function to set up the approriate type of Info. + std::unique_ptr readBlockToInfo(unsigned ID); + + llvm::BitstreamCursor &Stream; + Optional BlockInfo; + FieldId CurrentReferenceField; +}; + +} // namespace doc +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEREADER_H Index: clang-tools-extra/trunk/clang-doc/BitcodeReader.cpp =================================================================== --- clang-tools-extra/trunk/clang-doc/BitcodeReader.cpp +++ clang-tools-extra/trunk/clang-doc/BitcodeReader.cpp @@ -0,0 +1,622 @@ +//===-- BitcodeReader.cpp - ClangDoc Bitcode Reader ------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "BitcodeReader.h" +#include "llvm/ADT/IndexedMap.h" +#include "llvm/ADT/Optional.h" +#include "llvm/Support/raw_ostream.h" + +namespace clang { +namespace doc { + +using Record = llvm::SmallVector; + +bool decodeRecord(Record R, llvm::SmallVectorImpl &Field, + llvm::StringRef Blob) { + Field.assign(Blob.begin(), Blob.end()); + return true; +} + +bool decodeRecord(Record R, SymbolID &Field, llvm::StringRef Blob) { + if (R[0] != BitCodeConstants::USRHashSize) + return false; + + // First position in the record is the length of the following array, so we + // copy the following elements to the field. + for (int I = 0, E = R[0]; I < E; ++I) + Field[I] = R[I + 1]; + return true; +} + +bool decodeRecord(Record R, bool &Field, llvm::StringRef Blob) { + Field = R[0] != 0; + return true; +} + +bool decodeRecord(Record R, int &Field, llvm::StringRef Blob) { + if (R[0] > INT_MAX) + return false; + Field = (int)R[0]; + return true; +} + +bool decodeRecord(Record R, AccessSpecifier &Field, llvm::StringRef Blob) { + switch (R[0]) { + case AS_public: + case AS_private: + case AS_protected: + case AS_none: + Field = (AccessSpecifier)R[0]; + return true; + default: + return false; + } +} + +bool decodeRecord(Record R, TagTypeKind &Field, llvm::StringRef Blob) { + switch (R[0]) { + case TTK_Struct: + case TTK_Interface: + case TTK_Union: + case TTK_Class: + case TTK_Enum: + Field = (TagTypeKind)R[0]; + return true; + default: + return false; + } +} + +bool decodeRecord(Record R, llvm::Optional &Field, + llvm::StringRef Blob) { + if (R[0] > INT_MAX) + return false; + Field.emplace((int)R[0], Blob); + return true; +} + +bool decodeRecord(Record R, InfoType &Field, llvm::StringRef Blob) { + switch (auto IT = static_cast(R[0])) { + case InfoType::IT_namespace: + case InfoType::IT_record: + case InfoType::IT_function: + case InfoType::IT_default: + case InfoType::IT_enum: + Field = IT; + return true; + } + return false; +} + +bool decodeRecord(Record R, FieldId &Field, llvm::StringRef Blob) { + switch (auto F = static_cast(R[0])) { + case FieldId::F_namespace: + case FieldId::F_parent: + case FieldId::F_vparent: + case FieldId::F_type: + case FieldId::F_default: + Field = F; + return true; + } + return false; +} + +bool decodeRecord(Record R, llvm::SmallVectorImpl> &Field, + llvm::StringRef Blob) { + Field.push_back(Blob); + return true; +} + +bool decodeRecord(Record R, llvm::SmallVectorImpl &Field, + llvm::StringRef Blob) { + if (R[0] > INT_MAX) + return false; + Field.emplace_back((int)R[0], Blob); + return true; +} + +bool parseRecord(Record R, unsigned ID, llvm::StringRef Blob, + const unsigned VersionNo) { + if (ID == VERSION && R[0] == VersionNo) + return true; + return false; +} + +bool parseRecord(Record R, unsigned ID, llvm::StringRef Blob, + NamespaceInfo *I) { + switch (ID) { + case NAMESPACE_USR: + return decodeRecord(R, I->USR, Blob); + case NAMESPACE_NAME: + return decodeRecord(R, I->Name, Blob); + default: + return false; + } +} + +bool parseRecord(Record R, unsigned ID, llvm::StringRef Blob, RecordInfo *I) { + switch (ID) { + case RECORD_USR: + return decodeRecord(R, I->USR, Blob); + case RECORD_NAME: + return decodeRecord(R, I->Name, Blob); + case RECORD_DEFLOCATION: + return decodeRecord(R, I->DefLoc, Blob); + case RECORD_LOCATION: + return decodeRecord(R, I->Loc, Blob); + case RECORD_TAG_TYPE: + return decodeRecord(R, I->TagType, Blob); + default: + return false; + } +} + +bool parseRecord(Record R, unsigned ID, llvm::StringRef Blob, EnumInfo *I) { + switch (ID) { + case ENUM_USR: + return decodeRecord(R, I->USR, Blob); + case ENUM_NAME: + return decodeRecord(R, I->Name, Blob); + case ENUM_DEFLOCATION: + return decodeRecord(R, I->DefLoc, Blob); + case ENUM_LOCATION: + return decodeRecord(R, I->Loc, Blob); + case ENUM_MEMBER: + return decodeRecord(R, I->Members, Blob); + case ENUM_SCOPED: + return decodeRecord(R, I->Scoped, Blob); + default: + return false; + } +} + +bool parseRecord(Record R, unsigned ID, llvm::StringRef Blob, FunctionInfo *I) { + switch (ID) { + case FUNCTION_USR: + return decodeRecord(R, I->USR, Blob); + case FUNCTION_NAME: + return decodeRecord(R, I->Name, Blob); + case FUNCTION_DEFLOCATION: + return decodeRecord(R, I->DefLoc, Blob); + case FUNCTION_LOCATION: + return decodeRecord(R, I->Loc, Blob); + case FUNCTION_ACCESS: + return decodeRecord(R, I->Access, Blob); + case FUNCTION_IS_METHOD: + return decodeRecord(R, I->IsMethod, Blob); + default: + return false; + } +} + +bool parseRecord(Record R, unsigned ID, llvm::StringRef Blob, TypeInfo *I) { + return true; +} + +bool parseRecord(Record R, unsigned ID, llvm::StringRef Blob, + FieldTypeInfo *I) { + switch (ID) { + case FIELD_TYPE_NAME: + return decodeRecord(R, I->Name, Blob); + default: + return false; + } +} + +bool parseRecord(Record R, unsigned ID, llvm::StringRef Blob, + MemberTypeInfo *I) { + switch (ID) { + case MEMBER_TYPE_NAME: + return decodeRecord(R, I->Name, Blob); + case MEMBER_TYPE_ACCESS: + return decodeRecord(R, I->Access, Blob); + default: + return false; + } +} + +bool parseRecord(Record R, unsigned ID, llvm::StringRef Blob, CommentInfo *I) { + switch (ID) { + case COMMENT_KIND: + return decodeRecord(R, I->Kind, Blob); + case COMMENT_TEXT: + return decodeRecord(R, I->Text, Blob); + case COMMENT_NAME: + return decodeRecord(R, I->Name, Blob); + case COMMENT_DIRECTION: + return decodeRecord(R, I->Direction, Blob); + case COMMENT_PARAMNAME: + return decodeRecord(R, I->ParamName, Blob); + case COMMENT_CLOSENAME: + return decodeRecord(R, I->CloseName, Blob); + case COMMENT_ATTRKEY: + return decodeRecord(R, I->AttrKeys, Blob); + case COMMENT_ATTRVAL: + return decodeRecord(R, I->AttrValues, Blob); + case COMMENT_ARG: + return decodeRecord(R, I->Args, Blob); + case COMMENT_SELFCLOSING: + return decodeRecord(R, I->SelfClosing, Blob); + case COMMENT_EXPLICIT: + return decodeRecord(R, I->Explicit, Blob); + default: + return false; + } +} + +bool parseRecord(Record R, unsigned ID, llvm::StringRef Blob, Reference *I, + FieldId &F) { + switch (ID) { + case REFERENCE_USR: + return decodeRecord(R, I->USR, Blob); + case REFERENCE_NAME: + return decodeRecord(R, I->Name, Blob); + case REFERENCE_TYPE: + return decodeRecord(R, I->RefType, Blob); + case REFERENCE_FIELD: + return decodeRecord(R, F, Blob); + default: + return false; + } +} + +template CommentInfo *getCommentInfo(T I) { + llvm::errs() << "Cannot have comment subblock.\n"; + exit(1); +} + +template <> CommentInfo *getCommentInfo(FunctionInfo *I) { + I->Description.emplace_back(); + return &I->Description.back(); +} + +template <> CommentInfo *getCommentInfo(NamespaceInfo *I) { + I->Description.emplace_back(); + return &I->Description.back(); +} + +template <> CommentInfo *getCommentInfo(RecordInfo *I) { + I->Description.emplace_back(); + return &I->Description.back(); +} + +template <> CommentInfo *getCommentInfo(EnumInfo *I) { + I->Description.emplace_back(); + return &I->Description.back(); +} + +template <> CommentInfo *getCommentInfo(CommentInfo *I) { + I->Children.emplace_back(llvm::make_unique()); + return I->Children.back().get(); +} + +template <> CommentInfo *getCommentInfo(std::unique_ptr &I) { + return getCommentInfo(I.get()); +} + +template +void addTypeInfo(T I, TTypeInfo &&TI) { + llvm::errs() << "Invalid type for info.\n"; + exit(1); +} + +template <> void addTypeInfo(RecordInfo *I, MemberTypeInfo &&T) { + I->Members.emplace_back(std::move(T)); +} + +template <> void addTypeInfo(FunctionInfo *I, TypeInfo &&T) { + I->ReturnType = std::move(T); +} + +template <> void addTypeInfo(FunctionInfo *I, FieldTypeInfo &&T) { + I->Params.emplace_back(std::move(T)); +} + +template void addReference(T I, Reference &&R, FieldId F) { + llvm::errs() << "Invalid field type for info.\n"; + exit(1); +} + +template <> void addReference(TypeInfo *I, Reference &&R, FieldId F) { + switch (F) { + case FieldId::F_type: + I->Type = std::move(R); + break; + default: + llvm::errs() << "Invalid field type for info.\n"; + exit(1); + } +} + +template <> void addReference(FieldTypeInfo *I, Reference &&R, FieldId F) { + switch (F) { + case FieldId::F_type: + I->Type = std::move(R); + break; + default: + llvm::errs() << "Invalid field type for info.\n"; + exit(1); + } +} + +template <> void addReference(MemberTypeInfo *I, Reference &&R, FieldId F) { + switch (F) { + case FieldId::F_type: + I->Type = std::move(R); + break; + default: + llvm::errs() << "Invalid field type for info.\n"; + exit(1); + } +} + +template <> void addReference(EnumInfo *I, Reference &&R, FieldId F) { + switch (F) { + case FieldId::F_namespace: + I->Namespace.emplace_back(std::move(R)); + break; + default: + llvm::errs() << "Invalid field type for info.\n"; + exit(1); + } +} + +template <> void addReference(NamespaceInfo *I, Reference &&R, FieldId F) { + switch (F) { + case FieldId::F_namespace: + I->Namespace.emplace_back(std::move(R)); + break; + default: + llvm::errs() << "Invalid field type for info.\n"; + exit(1); + } +} + +template <> void addReference(FunctionInfo *I, Reference &&R, FieldId F) { + switch (F) { + case FieldId::F_namespace: + I->Namespace.emplace_back(std::move(R)); + break; + case FieldId::F_parent: + I->Parent = std::move(R); + break; + default: + llvm::errs() << "Invalid field type for info.\n"; + exit(1); + } +} + +template <> void addReference(RecordInfo *I, Reference &&R, FieldId F) { + switch (F) { + case FieldId::F_namespace: + I->Namespace.emplace_back(std::move(R)); + break; + case FieldId::F_parent: + I->Parents.emplace_back(std::move(R)); + break; + case FieldId::F_vparent: + I->VirtualParents.emplace_back(std::move(R)); + break; + default: + llvm::errs() << "Invalid field type for info.\n"; + exit(1); + } +} + +// Read records from bitcode into a given info. +template bool ClangDocBitcodeReader::readRecord(unsigned ID, T I) { + Record R; + llvm::StringRef Blob; + unsigned RecID = Stream.readRecord(ID, R, &Blob); + return parseRecord(R, RecID, Blob, I); +} + +template <> bool ClangDocBitcodeReader::readRecord(unsigned ID, Reference *I) { + Record R; + llvm::StringRef Blob; + unsigned RecID = Stream.readRecord(ID, R, &Blob); + return parseRecord(R, RecID, Blob, I, CurrentReferenceField); +} + +// Read a block of records into a single info. +template bool ClangDocBitcodeReader::readBlock(unsigned ID, T I) { + if (Stream.EnterSubBlock(ID)) + return false; + + while (true) { + unsigned BlockOrCode = 0; + Cursor Res = skipUntilRecordOrBlock(BlockOrCode); + + switch (Res) { + case Cursor::BadBlock: + return false; + case Cursor::BlockEnd: + return true; + case Cursor::BlockBegin: + if (readSubBlock(BlockOrCode, I)) + continue; + if (!Stream.SkipBlock()) + return false; + continue; + case Cursor::Record: + break; + } + if (!readRecord(BlockOrCode, I)) + return false; + } +} + +template +bool ClangDocBitcodeReader::readSubBlock(unsigned ID, T I) { + switch (ID) { + // Blocks can only have Comment, Reference, or TypeInfo subblocks + case BI_COMMENT_BLOCK_ID: + if (readBlock(ID, getCommentInfo(I))) + return true; + return false; + case BI_TYPE_BLOCK_ID: { + TypeInfo TI; + if (readBlock(ID, &TI)) { + addTypeInfo(I, std::move(TI)); + return true; + } + return false; + } + case BI_FIELD_TYPE_BLOCK_ID: { + FieldTypeInfo TI; + if (readBlock(ID, &TI)) { + addTypeInfo(I, std::move(TI)); + return true; + } + return false; + } + case BI_MEMBER_TYPE_BLOCK_ID: { + MemberTypeInfo TI; + if (readBlock(ID, &TI)) { + addTypeInfo(I, std::move(TI)); + return true; + } + return false; + } + case BI_REFERENCE_BLOCK_ID: { + Reference R; + if (readBlock(ID, &R)) { + addReference(I, std::move(R), CurrentReferenceField); + return true; + } + return false; + } + default: + llvm::errs() << "Invalid subblock type.\n"; + return false; + } +} + +ClangDocBitcodeReader::Cursor +ClangDocBitcodeReader::skipUntilRecordOrBlock(unsigned &BlockOrRecordID) { + BlockOrRecordID = 0; + + while (!Stream.AtEndOfStream()) { + unsigned Code = Stream.ReadCode(); + + switch ((llvm::bitc::FixedAbbrevIDs)Code) { + case llvm::bitc::ENTER_SUBBLOCK: + BlockOrRecordID = Stream.ReadSubBlockID(); + return Cursor::BlockBegin; + case llvm::bitc::END_BLOCK: + if (Stream.ReadBlockEnd()) + return Cursor::BadBlock; + return Cursor::BlockEnd; + case llvm::bitc::DEFINE_ABBREV: + Stream.ReadAbbrevRecord(); + continue; + case llvm::bitc::UNABBREV_RECORD: + return Cursor::BadBlock; + default: + BlockOrRecordID = Code; + return Cursor::Record; + } + } + llvm_unreachable("Premature stream end."); +} + +bool ClangDocBitcodeReader::validateStream() { + if (Stream.AtEndOfStream()) + return false; + + // Sniff for the signature. + if (Stream.Read(8) != BitCodeConstants::Signature[0] || + Stream.Read(8) != BitCodeConstants::Signature[1] || + Stream.Read(8) != BitCodeConstants::Signature[2] || + Stream.Read(8) != BitCodeConstants::Signature[3]) + return false; + return true; +} + +bool ClangDocBitcodeReader::readBlockInfoBlock() { + BlockInfo = Stream.ReadBlockInfoBlock(); + if (!BlockInfo) + return false; + Stream.setBlockInfo(&*BlockInfo); + return true; +} + +template +std::unique_ptr ClangDocBitcodeReader::createInfo(unsigned ID) { + std::unique_ptr I = llvm::make_unique(); + if (readBlock(ID, static_cast(I.get()))) + return I; + llvm::errs() << "Error reading from block.\n"; + return nullptr; +} + +std::unique_ptr ClangDocBitcodeReader::readBlockToInfo(unsigned ID) { + switch (ID) { + case BI_NAMESPACE_BLOCK_ID: + return createInfo(ID); + case BI_RECORD_BLOCK_ID: + return createInfo(ID); + case BI_ENUM_BLOCK_ID: + return createInfo(ID); + case BI_FUNCTION_BLOCK_ID: + return createInfo(ID); + default: + llvm::errs() << "Error reading from block.\n"; + return nullptr; + } +} + +// Entry point +std::vector> ClangDocBitcodeReader::readBitcode() { + std::vector> Infos; + if (!validateStream()) + return Infos; + + // Read the top level blocks. + while (!Stream.AtEndOfStream()) { + unsigned Code = Stream.ReadCode(); + if (Code != llvm::bitc::ENTER_SUBBLOCK) + return Infos; + + unsigned ID = Stream.ReadSubBlockID(); + switch (ID) { + // NamedType and Comment blocks should not appear at the top level + case BI_TYPE_BLOCK_ID: + case BI_FIELD_TYPE_BLOCK_ID: + case BI_MEMBER_TYPE_BLOCK_ID: + case BI_COMMENT_BLOCK_ID: + case BI_REFERENCE_BLOCK_ID: + llvm::errs() << "Invalid top level block.\n"; + return Infos; + case BI_NAMESPACE_BLOCK_ID: + case BI_RECORD_BLOCK_ID: + case BI_ENUM_BLOCK_ID: + case BI_FUNCTION_BLOCK_ID: + if (std::unique_ptr I = readBlockToInfo(ID)) { + Infos.emplace_back(std::move(I)); + } + return Infos; + case BI_VERSION_BLOCK_ID: + if (readBlock(ID, VersionNumber)) + continue; + return Infos; + case llvm::bitc::BLOCKINFO_BLOCK_ID: + if (readBlockInfoBlock()) + continue; + return Infos; + default: + if (!Stream.SkipBlock()) + continue; + } + } + return Infos; +} + +} // namespace doc +} // namespace clang Index: clang-tools-extra/trunk/clang-doc/BitcodeWriter.h =================================================================== --- clang-tools-extra/trunk/clang-doc/BitcodeWriter.h +++ clang-tools-extra/trunk/clang-doc/BitcodeWriter.h @@ -34,7 +34,7 @@ static const unsigned VersionNumber = 2; struct BitCodeConstants { - static constexpr unsigned RecordSize = 16U; + static constexpr unsigned RecordSize = 32U; static constexpr unsigned SignatureBitSize = 8U; static constexpr unsigned SubblockIDSize = 4U; static constexpr unsigned BoolSize = 1U; @@ -45,6 +45,8 @@ static constexpr unsigned ReferenceTypeSize = 8U; static constexpr unsigned USRLengthSize = 6U; static constexpr unsigned USRBitLengthSize = 8U; + static constexpr char Signature[4] = {'D', 'O', 'C', 'S'}; + static constexpr int USRHashSize = 20; }; // New Ids need to be added to both the enum here and the relevant IdNameMap in @@ -113,7 +115,7 @@ #undef INFORECORDS // Identifiers for differentiating between subblocks -enum class FieldId { F_namespace = 1, F_parent, F_vparent, F_type }; +enum class FieldId { F_default, F_namespace, F_parent, F_vparent, F_type }; class ClangDocBitcodeWriter { public: @@ -123,12 +125,8 @@ emitVersionBlock(); } -#ifndef NDEBUG // Don't want explicit dtor unless needed. - ~ClangDocBitcodeWriter() { - // Check that the static size is large-enough. - assert(Record.capacity() > BitCodeConstants::RecordSize); - } -#endif + // Write a specific info to a bitcode stream. + bool dispatchInfoForWrite(Info *I); // Block emission of different info types. void emitBlock(const NamespaceInfo &I); Index: clang-tools-extra/trunk/clang-doc/BitcodeWriter.cpp =================================================================== --- clang-tools-extra/trunk/clang-doc/BitcodeWriter.cpp +++ clang-tools-extra/trunk/clang-doc/BitcodeWriter.cpp @@ -214,6 +214,8 @@ // AbbreviationMap +constexpr char BitCodeConstants::Signature[]; + void ClangDocBitcodeWriter::AbbreviationMap::add(RecordId RID, unsigned AbbrevID) { assert(RecordIdNameMap[RID] && "Unknown RecordId."); @@ -232,7 +234,7 @@ /// \brief Emits the magic number header to check that its the right format, /// in this case, 'DOCS'. void ClangDocBitcodeWriter::emitHeader() { - for (char C : llvm::StringRef("DOCS")) + for (char C : BitCodeConstants::Signature) Stream.Emit((unsigned)C, BitCodeConstants::SignatureBitSize); } @@ -424,22 +426,24 @@ emitBlock(*C); } -#define EMITINFO(X) \ - emitRecord(I.USR, X##_USR); \ - emitRecord(I.Name, X##_NAME); \ - for (const auto &N : I.Namespace) \ - emitBlock(N, FieldId::F_namespace); \ - for (const auto &CI : I.Description) \ - emitBlock(CI); - void ClangDocBitcodeWriter::emitBlock(const NamespaceInfo &I) { StreamSubBlockGuard Block(Stream, BI_NAMESPACE_BLOCK_ID); - EMITINFO(NAMESPACE) + emitRecord(I.USR, NAMESPACE_USR); + emitRecord(I.Name, NAMESPACE_NAME); + for (const auto &N : I.Namespace) + emitBlock(N, FieldId::F_namespace); + for (const auto &CI : I.Description) + emitBlock(CI); } void ClangDocBitcodeWriter::emitBlock(const EnumInfo &I) { StreamSubBlockGuard Block(Stream, BI_ENUM_BLOCK_ID); - EMITINFO(ENUM) + emitRecord(I.USR, ENUM_USR); + emitRecord(I.Name, ENUM_NAME); + for (const auto &N : I.Namespace) + emitBlock(N, FieldId::F_namespace); + for (const auto &CI : I.Description) + emitBlock(CI); if (I.DefLoc) emitRecord(I.DefLoc.getValue(), ENUM_DEFLOCATION); for (const auto &L : I.Loc) @@ -451,7 +455,12 @@ void ClangDocBitcodeWriter::emitBlock(const RecordInfo &I) { StreamSubBlockGuard Block(Stream, BI_RECORD_BLOCK_ID); - EMITINFO(RECORD) + emitRecord(I.USR, RECORD_USR); + emitRecord(I.Name, RECORD_NAME); + for (const auto &N : I.Namespace) + emitBlock(N, FieldId::F_namespace); + for (const auto &CI : I.Description) + emitBlock(CI); if (I.DefLoc) emitRecord(I.DefLoc.getValue(), RECORD_DEFLOCATION); for (const auto &L : I.Loc) @@ -467,7 +476,12 @@ void ClangDocBitcodeWriter::emitBlock(const FunctionInfo &I) { StreamSubBlockGuard Block(Stream, BI_FUNCTION_BLOCK_ID); - EMITINFO(FUNCTION) + emitRecord(I.USR, FUNCTION_USR); + emitRecord(I.Name, FUNCTION_NAME); + for (const auto &N : I.Namespace) + emitBlock(N, FieldId::F_namespace); + for (const auto &CI : I.Description) + emitBlock(CI); emitRecord(I.IsMethod, FUNCTION_IS_METHOD); if (I.DefLoc) emitRecord(I.DefLoc.getValue(), FUNCTION_DEFLOCATION); @@ -479,7 +493,26 @@ emitBlock(N); } -#undef EMITINFO +bool ClangDocBitcodeWriter::dispatchInfoForWrite(Info *I) { + switch (I->IT) { + case InfoType::IT_namespace: + emitBlock(*static_cast(I)); + break; + case InfoType::IT_record: + emitBlock(*static_cast(I)); + break; + case InfoType::IT_enum: + emitBlock(*static_cast(I)); + break; + case InfoType::IT_function: + emitBlock(*static_cast(I)); + break; + default: + llvm::errs() << "Unexpected info, unable to write.\n"; + return true; + } + return false; +} } // namespace doc } // namespace clang Index: clang-tools-extra/trunk/clang-doc/CMakeLists.txt =================================================================== --- clang-tools-extra/trunk/clang-doc/CMakeLists.txt +++ clang-tools-extra/trunk/clang-doc/CMakeLists.txt @@ -3,9 +3,11 @@ ) add_clang_library(clangDoc + BitcodeReader.cpp BitcodeWriter.cpp ClangDoc.cpp Mapper.cpp + Representation.cpp Serialize.cpp LINK_LIBS Index: clang-tools-extra/trunk/clang-doc/Representation.h =================================================================== --- clang-tools-extra/trunk/clang-doc/Representation.h +++ clang-tools-extra/trunk/clang-doc/Representation.h @@ -1,4 +1,4 @@ -///===-- Representation.h - ClangDoc Represenation --------------*- C++ -*-===// +///===-- Representation.h - ClangDoc Representation -------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -26,6 +26,7 @@ namespace clang { namespace doc { +// SHA1'd hash of a USR. using SymbolID = std::array; struct Info; @@ -40,7 +41,8 @@ // A representation of a parsed comment. struct CommentInfo { CommentInfo() = default; - CommentInfo(CommentInfo &&Other) : Children(std::move(Other.Children)) {} + CommentInfo(CommentInfo &Other) = delete; + CommentInfo(CommentInfo &&Other) = default; SmallString<16> Kind; // Kind of comment (TextComment, InlineCommandComment, // HTMLStartTagComment, HTMLEndTagComment, @@ -128,21 +130,35 @@ /// A base struct for Infos. struct Info { Info() = default; - Info(Info &&Other) : Description(std::move(Other.Description)) {} - virtual ~Info() = default; - - SymbolID USR; // Unique identifier for the decl described by this Info. - SmallString<16> Name; // Unqualified name of the decl. + Info(InfoType IT) : IT(IT) {} + Info(const Info &Other) = delete; + Info(Info &&Other) = default; + + SymbolID USR = + SymbolID(); // Unique identifier for the decl described by this Info. + const InfoType IT = InfoType::IT_default; // InfoType of this particular Info. + SmallString<16> Name; // Unqualified name of the decl. llvm::SmallVector Namespace; // List of parent namespaces for this decl. std::vector Description; // Comment description of this decl. + + void mergeBase(Info &&I); + bool mergeable(const Info &Other); }; // Info for namespaces. -struct NamespaceInfo : public Info {}; +struct NamespaceInfo : public Info { + NamespaceInfo() : Info(InfoType::IT_namespace) {} + + void merge(NamespaceInfo &&I); +}; // Info for symbols. struct SymbolInfo : public Info { + SymbolInfo(InfoType IT) : Info(IT) {} + + void merge(SymbolInfo &&I); + llvm::Optional DefLoc; // Location where this decl is defined. llvm::SmallVector Loc; // Locations where this decl is declared. }; @@ -150,6 +166,10 @@ // TODO: Expand to allow for documenting templating and default args. // Info for functions. struct FunctionInfo : public SymbolInfo { + FunctionInfo() : SymbolInfo(InfoType::IT_function) {} + + void merge(FunctionInfo &&I); + bool IsMethod = false; // Indicates whether this function is a class method. Reference Parent; // Reference to the parent class decl for this method. TypeInfo ReturnType; // Info about the return type of this function. @@ -163,12 +183,18 @@ // friend classes // Info for types. struct RecordInfo : public SymbolInfo { - TagTypeKind TagType = TagTypeKind::TTK_Struct; // Type of this record (struct, - // class, union, interface). + RecordInfo() : SymbolInfo(InfoType::IT_record) {} + + void merge(RecordInfo &&I); + + TagTypeKind TagType = TagTypeKind::TTK_Struct; // Type of this record + // (struct, class, union, + // interface). llvm::SmallVector Members; // List of info about record members. - llvm::SmallVector Parents; // List of base/parent records (does - // not include virtual parents). + llvm::SmallVector Parents; // List of base/parent records + // (does not include virtual + // parents). llvm::SmallVector VirtualParents; // List of virtual base/parent records. }; @@ -176,6 +202,10 @@ // TODO: Expand to allow for documenting templating. // Info for types. struct EnumInfo : public SymbolInfo { + EnumInfo() : SymbolInfo(InfoType::IT_enum) {} + + void merge(EnumInfo &&I); + bool Scoped = false; // Indicates whether this enum is scoped (e.g. enum class). llvm::SmallVector, 4> Members; // List of enum members. @@ -183,6 +213,12 @@ // TODO: Add functionality to include separate markdown pages. +// A standalone function to call to merge a vector of infos into one. +// This assumes that all infos in the vector are of the same type, and will fail +// if they are different. +llvm::Expected> +mergeInfos(std::vector> &Values); + } // namespace doc } // namespace clang Index: clang-tools-extra/trunk/clang-doc/Representation.cpp =================================================================== --- clang-tools-extra/trunk/clang-doc/Representation.cpp +++ clang-tools-extra/trunk/clang-doc/Representation.cpp @@ -0,0 +1,131 @@ +///===-- Representation.cpp - ClangDoc Representation -----------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines the merging of different types of infos. The data in the +// calling Info is preserved during a merge unless that field is empty or +// default. In that case, the data from the parameter Info is used to replace +// the empty or default data. +// +// For most fields, the first decl seen provides the data. Exceptions to this +// include the location and description fields, which are collections of data on +// all decls related to a given definition. All other fields are ignored in new +// decls unless the first seen decl didn't, for whatever reason, incorporate +// data on that field (e.g. a forward declared class wouldn't have information +// on members on the forward declaration, but would have the class name). +// +//===----------------------------------------------------------------------===// +#include "Representation.h" +#include "llvm/Support/Error.h" + +namespace clang { +namespace doc { + +static const SymbolID EmptySID = SymbolID(); + +template +std::unique_ptr reduce(std::vector> &Values) { + std::unique_ptr Merged = llvm::make_unique(); + T *Tmp = static_cast(Merged.get()); + for (auto &I : Values) + Tmp->merge(std::move(*static_cast(I.get()))); + return Merged; +} + +// Dispatch function. +llvm::Expected> +mergeInfos(std::vector> &Values) { + if (Values.empty()) + return llvm::make_error("No info values to merge.\n", + llvm::inconvertibleErrorCode()); + + switch (Values[0]->IT) { + case InfoType::IT_namespace: + return reduce(Values); + case InfoType::IT_record: + return reduce(Values); + case InfoType::IT_enum: + return reduce(Values); + case InfoType::IT_function: + return reduce(Values); + default: + return llvm::make_error("Unexpected info type.\n", + llvm::inconvertibleErrorCode()); + } +} + +void Info::mergeBase(Info &&Other) { + assert(mergeable(Other)); + if (USR == EmptySID) + USR = Other.USR; + if (Name == "") + Name = Other.Name; + if (Namespace.empty()) + Namespace = std::move(Other.Namespace); + // Unconditionally extend the description, since each decl may have a comment. + std::move(Other.Description.begin(), Other.Description.end(), + std::back_inserter(Description)); +} + +bool Info::mergeable(const Info &Other) { + return IT == Other.IT && (USR == EmptySID || USR == Other.USR); +} + +void SymbolInfo::merge(SymbolInfo &&Other) { + assert(mergeable(Other)); + if (!DefLoc) + DefLoc = std::move(Other.DefLoc); + // Unconditionally extend the list of locations, since we want all of them. + std::move(Other.Loc.begin(), Other.Loc.end(), std::back_inserter(Loc)); + mergeBase(std::move(Other)); +} + +void NamespaceInfo::merge(NamespaceInfo &&Other) { + assert(mergeable(Other)); + mergeBase(std::move(Other)); +} + +void RecordInfo::merge(RecordInfo &&Other) { + assert(mergeable(Other)); + if (!TagType) + TagType = Other.TagType; + if (Members.empty()) + Members = std::move(Other.Members); + if (Parents.empty()) + Parents = std::move(Other.Parents); + if (VirtualParents.empty()) + VirtualParents = std::move(Other.VirtualParents); + SymbolInfo::merge(std::move(Other)); +} + +void EnumInfo::merge(EnumInfo &&Other) { + assert(mergeable(Other)); + if (!Scoped) + Scoped = Other.Scoped; + if (Members.empty()) + Members = std::move(Other.Members); + SymbolInfo::merge(std::move(Other)); +} + +void FunctionInfo::merge(FunctionInfo &&Other) { + assert(mergeable(Other)); + if (!IsMethod) + IsMethod = Other.IsMethod; + if (!Access) + Access = Other.Access; + if (ReturnType.Type.USR == EmptySID && ReturnType.Type.Name == "") + ReturnType = std::move(Other.ReturnType); + if (Parent.USR == EmptySID && Parent.Name == "") + Parent = std::move(Other.Parent); + if (Params.empty()) + Params = std::move(Other.Params); + SymbolInfo::merge(std::move(Other)); +} + +} // namespace doc +} // namespace clang Index: clang-tools-extra/trunk/clang-doc/tool/ClangDocMain.cpp =================================================================== --- clang-tools-extra/trunk/clang-doc/tool/ClangDocMain.cpp +++ clang-tools-extra/trunk/clang-doc/tool/ClangDocMain.cpp @@ -18,7 +18,10 @@ // //===----------------------------------------------------------------------===// +#include "BitcodeReader.h" +#include "BitcodeWriter.h" #include "ClangDoc.h" +#include "Representation.h" #include "clang/AST/AST.h" #include "clang/AST/Decl.h" #include "clang/ASTMatchers/ASTMatchFinder.h" @@ -30,6 +33,7 @@ #include "clang/Tooling/StandaloneExecution.h" #include "clang/Tooling/Tooling.h" #include "llvm/ADT/APFloat.h" +#include "llvm/Support/Error.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Path.h" #include "llvm/Support/Process.h" @@ -54,14 +58,66 @@ llvm::cl::desc("Dump mapper results to bitcode file."), llvm::cl::init(false), llvm::cl::cat(ClangDocCategory)); +static llvm::cl::opt DumpIntermediateResult( + "dump-intermediate", + llvm::cl::desc("Dump intermediate results to bitcode file."), + llvm::cl::init(false), llvm::cl::cat(ClangDocCategory)); + +enum OutputFormatTy { + yaml, +}; + +static llvm::cl::opt + Format("format", llvm::cl::desc("Format for outputted docs."), + llvm::cl::values(clEnumVal(yaml, "Documentation in YAML format.")), + llvm::cl::init(yaml), llvm::cl::cat(ClangDocCategory)); + static llvm::cl::opt DoxygenOnly( "doxygen", llvm::cl::desc("Use only doxygen-style comments to generate docs."), llvm::cl::init(false), llvm::cl::cat(ClangDocCategory)); +bool CreateDirectory(const Twine &DirName, bool ClearDirectory = false) { + std::error_code OK; + llvm::SmallString<128> DocsRootPath; + if (ClearDirectory) { + std::error_code RemoveStatus = llvm::sys::fs::remove_directories(DirName); + if (RemoveStatus != OK) { + llvm::errs() << "Unable to remove existing documentation directory for " + << DirName << ".\n"; + return true; + } + } + std::error_code DirectoryStatus = llvm::sys::fs::create_directories(DirName); + if (DirectoryStatus != OK) { + llvm::errs() << "Unable to create documentation directories.\n"; + return true; + } + return false; +} + +bool DumpResultToFile(const Twine &DirName, const Twine &FileName, + StringRef Buffer, bool ClearDirectory = false) { + std::error_code OK; + llvm::SmallString<128> IRRootPath; + llvm::sys::path::native(OutDirectory, IRRootPath); + llvm::sys::path::append(IRRootPath, DirName); + if (CreateDirectory(IRRootPath, ClearDirectory)) + return true; + llvm::sys::path::append(IRRootPath, FileName); + std::error_code OutErrorInfo; + llvm::raw_fd_ostream OS(IRRootPath, OutErrorInfo, llvm::sys::fs::F_None); + if (OutErrorInfo != OK) { + llvm::errs() << "Error opening documentation file.\n"; + return true; + } + OS << Buffer; + OS.close(); + return false; +} + int main(int argc, const char **argv) { llvm::sys::PrintStackTraceOnErrorSignal(argv[0]); - std::error_code OK; auto Exec = clang::tooling::createExecutorFromCommandLineArgs( argc, argv, ClangDocCategory); @@ -80,34 +136,66 @@ // Mapping phase llvm::outs() << "Mapping decls...\n"; - auto Err = Exec->get()->execute(doc::newMapperActionFactory( - Exec->get()->getExecutionContext()), - ArgAdjuster); - if (Err) + auto Err = Exec->get()->execute( + doc::newMapperActionFactory(Exec->get()->getExecutionContext()), + ArgAdjuster); + if (Err) { llvm::errs() << toString(std::move(Err)) << "\n"; + return 1; + } if (DumpMapperResult) { - Exec->get()->getToolResults()->forEachResult([&](StringRef Key, - StringRef Value) { - SmallString<128> IRRootPath; - llvm::sys::path::native(OutDirectory, IRRootPath); - llvm::sys::path::append(IRRootPath, "bc"); - std::error_code DirectoryStatus = - llvm::sys::fs::create_directories(IRRootPath); - if (DirectoryStatus != OK) { - llvm::errs() << "Unable to create documentation directories.\n"; - return; - } - llvm::sys::path::append(IRRootPath, Key + ".bc"); - std::error_code OutErrorInfo; - llvm::raw_fd_ostream OS(IRRootPath, OutErrorInfo, llvm::sys::fs::F_None); - if (OutErrorInfo != OK) { - llvm::errs() << "Error opening documentation file.\n"; - return; + bool Err = false; + Exec->get()->getToolResults()->forEachResult( + [&](StringRef Key, StringRef Value) { + Err = DumpResultToFile("bc", Key + ".bc", Value); + }); + if (Err) + llvm::errs() << "Error dumping map results.\n"; + return Err; + } + + // Collect values into output by key. + llvm::outs() << "Collecting infos...\n"; + llvm::StringMap>> MapOutput; + + // In ToolResults, the Key is the hashed USR and the value is the + // bitcode-encoded representation of the Info object. + Exec->get()->getToolResults()->forEachResult([&](StringRef Key, + StringRef Value) { + llvm::BitstreamCursor Stream(Value); + doc::ClangDocBitcodeReader Reader(Stream); + auto Infos = Reader.readBitcode(); + for (auto &I : Infos) { + auto R = + MapOutput.try_emplace(Key, std::vector>()); + R.first->second.emplace_back(std::move(I)); + } + }); + + // Reducing phase + llvm::outs() << "Reducing " << MapOutput.size() << " infos...\n"; + llvm::StringMap> ReduceOutput; + for (auto &Group : MapOutput) { + auto Reduced = doc::mergeInfos(Group.getValue()); + if (!Reduced) + llvm::errs() << llvm::toString(Reduced.takeError()); + + if (DumpIntermediateResult) { + SmallString<4096> Buffer; + llvm::BitstreamWriter Stream(Buffer); + doc::ClangDocBitcodeWriter Writer(Stream); + Writer.dispatchInfoForWrite(Reduced.get().get()); + if (DumpResultToFile("bc", Group.getKey() + ".bc", Buffer)) { + llvm::errs() << "Error writing " << Group.getKey() << " to file.\n"; + continue; } - OS << Value; - OS.close(); - }); + } + + ReduceOutput.insert( + std::make_pair(Group.getKey(), std::move(Reduced.get()))); + + // FIXME: Add support for emitting different output formats. } return 0; Index: clang-tools-extra/trunk/test/clang-doc/bc-comment.cpp =================================================================== --- clang-tools-extra/trunk/test/clang-doc/bc-comment.cpp +++ clang-tools-extra/trunk/test/clang-doc/bc-comment.cpp @@ -0,0 +1,197 @@ +// RUN: rm -rf %t +// RUN: mkdir %t +// RUN: echo "" > %t/compile_flags.txt +// RUN: cp "%s" "%t/test.cpp" +// RUN: clang-doc --dump-intermediate -doxygen -p %t %t/test.cpp -output=%t/docs +// RUN: llvm-bcanalyzer %t/docs/bc/7574630614A535710E5A6ABCFFF98BCA2D06A4CA.bc --dump | FileCheck %s + +/// \brief Brief description. +/// +/// Extended description that +/// continues onto the next line. +/// +///
    +///
  • Testing. +///
+/// +/// \verbatim +/// The description continues. +/// \endverbatim +/// -- +/// \param [out] I is a parameter. +/// \param J is a parameter. +/// \return void +void F(int I, int J); + +/// Bonus comment on definition +void F(int I, int J) {} + +// CHECK: +// CHECK-NEXT: + // CHECK-NEXT: +// CHECK-NEXT: +// CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'F' + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'FullComment' + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'ParagraphComment' + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'TextComment' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'BlockCommandComment' + // CHECK-NEXT: blob data = 'brief' + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'ParagraphComment' + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'TextComment' + // CHECK-NEXT: blob data = ' Brief description.' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'ParagraphComment' + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'TextComment' + // CHECK-NEXT: blob data = ' Extended description that' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'TextComment' + // CHECK-NEXT: blob data = ' continues onto the next line.' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'ParagraphComment' + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'TextComment' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'HTMLStartTagComment' + // CHECK-NEXT: blob data = 'ul' + // CHECK-NEXT: blob data = 'class' + // CHECK-NEXT: blob data = 'test' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'TextComment' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'HTMLStartTagComment' + // CHECK-NEXT: blob data = 'li' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'TextComment' + // CHECK-NEXT: blob data = ' Testing.' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'TextComment' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'HTMLEndTagComment' + // CHECK-NEXT: blob data = 'ul' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'ParagraphComment' + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'TextComment' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'VerbatimBlockComment' + // CHECK-NEXT: blob data = 'verbatim' + // CHECK-NEXT: blob data = 'endverbatim' + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'VerbatimBlockLineComment' + // CHECK-NEXT: blob data = ' The description continues.' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'ParagraphComment' + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'TextComment' + // CHECK-NEXT: blob data = ' --' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'TextComment' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'ParamCommandComment' + // CHECK-NEXT: blob data = '[out]' + // CHECK-NEXT: blob data = 'I' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'ParagraphComment' + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'TextComment' + // CHECK-NEXT: blob data = ' is a parameter.' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'TextComment' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'ParamCommandComment' + // CHECK-NEXT: blob data = '[in]' + // CHECK-NEXT: blob data = 'J' + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'ParagraphComment' + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'TextComment' + // CHECK-NEXT: blob data = ' is a parameter.' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'TextComment' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'BlockCommandComment' + // CHECK-NEXT: blob data = 'return' + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'ParagraphComment' + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'TextComment' + // CHECK-NEXT: blob data = ' void' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'FullComment' + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'ParagraphComment' + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'TextComment' + // CHECK-NEXT: blob data = ' Bonus comment on definition' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = '{{.*}}' + // CHECK-NEXT: blob data = '{{.*}}' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'void' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'int' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'I' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'int' + // CHECK-NEXT: + // CHECK-NEXT: + // CHECK-NEXT: blob data = 'J' + // CHECK-NEXT: +// CHECK-NEXT: Index: clang-tools-extra/trunk/test/clang-doc/bc-namespace.cpp =================================================================== --- clang-tools-extra/trunk/test/clang-doc/bc-namespace.cpp +++ clang-tools-extra/trunk/test/clang-doc/bc-namespace.cpp @@ -0,0 +1,109 @@ +// RUN: rm -rf %t +// RUN: mkdir %t +// RUN: echo "" > %t/compile_flags.txt +// RUN: cp "%s" "%t/test.cpp" +// RUN: clang-doc --dump-intermediate -doxygen -p %t %t/test.cpp -output=%t/docs +// RUN: llvm-bcanalyzer %t/docs/bc/8D042EFFC98B373450BC6B5B90A330C25A150E9C.bc --dump | FileCheck %s --check-prefix CHECK-A +// RUN: llvm-bcanalyzer %t/docs/bc/E21AF79E2A9D02554BA090D10DF39FE273F5CDB5.bc --dump | FileCheck %s --check-prefix CHECK-B +// RUN: llvm-bcanalyzer %t/docs/bc/39D3C95A5F7CE2BA4937BD7B01BAE09EBC2AD8AC.bc --dump | FileCheck %s --check-prefix CHECK-F +// RUN: llvm-bcanalyzer %t/docs/bc/9A82CB33ED0FDF81EE383D31CD0957D153C5E840.bc --dump | FileCheck %s --check-prefix CHECK-FUNC +// RUN: llvm-bcanalyzer %t/docs/bc/E9ABF7E7E2425B626723D41E76E4BC7E7A5BD775.bc --dump | FileCheck %s --check-prefix CHECK-E + +namespace A { +// CHECK-A: + // CHECK-A-NEXT: + // CHECK-A-NEXT: blob data = 'A' +// CHECK-A-NEXT: + +void f(); + +} // namespace A + +namespace A { + +void f(){}; +// CHECK-F: + // CHECK-F-NEXT: + // CHECK-F-NEXT: blob data = 'f' + // CHECK-F-NEXT: + // CHECK-F-NEXT: + // CHECK-F-NEXT: blob data = 'A' + // CHECK-F-NEXT: + // CHECK-F-NEXT: + // CHECK-F-NEXT: + // CHECK-F-NEXT: blob data = '{{.*}}' + // CHECK-F-NEXT: blob data = '{{.*}}' + // CHECK-F-NEXT: + // CHECK-F-NEXT: + // CHECK-F-NEXT: blob data = 'void' + // CHECK-F-NEXT: + // CHECK-F-NEXT: + // CHECK-F-NEXT: +// CHECK-F-NEXT: + +namespace B { +// CHECK-B: + // CHECK-B-NEXT: + // CHECK-B-NEXT: blob data = 'B' + // CHECK-B-NEXT: + // CHECK-B-NEXT: + // CHECK-B-NEXT: blob data = 'A' + // CHECK-B-NEXT: + // CHECK-B-NEXT: + // CHECK-B-NEXT: +// CHECK-B-NEXT: + +enum E { X }; +// CHECK-E: + // CHECK-E-NEXT: + // CHECK-E-NEXT: blob data = 'E' + // CHECK-E-NEXT: + // CHECK-E-NEXT: + // CHECK-E-NEXT: blob data = 'B' + // CHECK-E-NEXT: + // CHECK-E-NEXT: + // CHECK-E-NEXT: + // CHECK-E-NEXT: + // CHECK-E-NEXT: + // CHECK-E-NEXT: blob data = 'A' + // CHECK-E-NEXT: + // CHECK-E-NEXT: + // CHECK-E-NEXT: + // CHECK-E-NEXT: blob data = '{{.*}}' + // CHECK-E-NEXT: blob data = 'X' +// CHECK-E-NEXT: + +E func(int i) { return X; } +// CHECK-FUNC: + // CHECK-FUNC-NEXT: + // CHECK-FUNC-NEXT: blob data = 'func' + // CHECK-FUNC-NEXT: + // CHECK-FUNC-NEXT: + // CHECK-FUNC-NEXT: blob data = 'B' + // CHECK-FUNC-NEXT: + // CHECK-FUNC-NEXT: + // CHECK-FUNC-NEXT: + // CHECK-FUNC-NEXT: + // CHECK-FUNC-NEXT: + // CHECK-FUNC-NEXT: blob data = 'A' + // CHECK-FUNC-NEXT: + // CHECK-FUNC-NEXT: + // CHECK-FUNC-NEXT: + // CHECK-FUNC-NEXT: blob data = '{{.*}}' + // CHECK-FUNC-NEXT: + // CHECK-FUNC-NEXT: + // CHECK-FUNC-NEXT: blob data = 'enum A::B::E' + // CHECK-FUNC-NEXT: + // CHECK-FUNC-NEXT: + // CHECK-FUNC-NEXT: + // CHECK-FUNC-NEXT: + // CHECK-FUNC-NEXT: + // CHECK-FUNC-NEXT: blob data = 'int' + // CHECK-FUNC-NEXT: + // CHECK-FUNC-NEXT: + // CHECK-FUNC-NEXT: blob data = 'i' + // CHECK-FUNC-NEXT: +// CHECK-FUNC-NEXT: + +} // namespace B +} // namespace A Index: clang-tools-extra/trunk/test/clang-doc/bc-record.cpp =================================================================== --- clang-tools-extra/trunk/test/clang-doc/bc-record.cpp +++ clang-tools-extra/trunk/test/clang-doc/bc-record.cpp @@ -0,0 +1,254 @@ +// This test requires Linux due to the system-dependent USR for the +// inner class in function H. +// REQUIRES: system-linux +// RUN: rm -rf %t +// RUN: mkdir %t +// RUN: echo "" > %t/compile_flags.txt +// RUN: cp "%s" "%t/test.cpp" +// RUN: clang-doc --dump-intermediate -doxygen -p %t %t/test.cpp -output=%t/docs +// RUN: llvm-bcanalyzer %t/docs/bc/ACE81AFA6627B4CEF2B456FB6E1252925674AF7E.bc --dump | FileCheck %s --check-prefix CHECK-A +// RUN: llvm-bcanalyzer %t/docs/bc/FC07BD34D5E77782C263FA944447929EA8753740.bc --dump | FileCheck %s --check-prefix CHECK-B +// RUN: llvm-bcanalyzer %t/docs/bc/1E3438A08BA22025C0B46289FF0686F92C8924C5.bc --dump | FileCheck %s --check-prefix CHECK-BC +// RUN: llvm-bcanalyzer %t/docs/bc/06B5F6A19BA9F6A832E127C9968282B94619B210.bc --dump | FileCheck %s --check-prefix CHECK-C +// RUN: llvm-bcanalyzer %t/docs/bc/0921737541208B8FA9BB42B60F78AC1D779AA054.bc --dump | FileCheck %s --check-prefix CHECK-D +// RUN: llvm-bcanalyzer %t/docs/bc/289584A8E0FF4178A794622A547AA622503967A1.bc --dump | FileCheck %s --check-prefix CHECK-E +// RUN: llvm-bcanalyzer %t/docs/bc/DEB4AC1CD9253CD9EF7FBE6BCAC506D77984ABD4.bc --dump | FileCheck %s --check-prefix CHECK-ECON +// RUN: llvm-bcanalyzer %t/docs/bc/BD2BDEBD423F80BACCEA75DE6D6622D355FC2D17.bc --dump | FileCheck %s --check-prefix CHECK-EDES +// RUN: llvm-bcanalyzer %t/docs/bc/E3B54702FABFF4037025BA194FC27C47006330B5.bc --dump | FileCheck %s --check-prefix CHECK-F +// RUN: llvm-bcanalyzer %t/docs/bc/B6AC4C5C9F2EA3F2B3ECE1A33D349F4EE502B24E.bc --dump | FileCheck %s --check-prefix CHECK-H +// RUN: llvm-bcanalyzer %t/docs/bc/6BA1EE2B3DAEACF6E4306F10AF44908F4807927C.bc --dump | FileCheck %s --check-prefix CHECK-I +// RUN: llvm-bcanalyzer %t/docs/bc/5093D428CDC62096A67547BA52566E4FB9404EEE.bc --dump | FileCheck %s --check-prefix CHECK-PM +// RUN: llvm-bcanalyzer %t/docs/bc/CA7C7935730B5EACD25F080E9C83FA087CCDC75E.bc --dump | FileCheck %s --check-prefix CHECK-X +// RUN: llvm-bcanalyzer %t/docs/bc/641AB4A3D36399954ACDE29C7A8833032BF40472.bc --dump | FileCheck %s --check-prefix CHECK-Y + +void H() { + class I {}; +} +// CHECK-H: + // CHECK-H-NEXT: + // CHECK-H-NEXT: blob data = 'H' + // CHECK-H-NEXT: blob data = '{{.*}}' + // CHECK-H-NEXT: + // CHECK-H-NEXT: + // CHECK-H-NEXT: blob data = 'void' + // CHECK-H-NEXT: + // CHECK-H-NEXT: + // CHECK-H-NEXT: +// CHECK-H-NEXT: + + +// CHECK-I: + // CHECK-I-NEXT: + // CHECK-I-NEXT: blob data = 'I' + // CHECK-I-NEXT: + // CHECK-I-NEXT: + // CHECK-I-NEXT: blob data = 'H' + // CHECK-I-NEXT: + // CHECK-I-NEXT: + // CHECK-I-NEXT: + // CHECK-I-NEXT: blob data = '{{.*}}' + // CHECK-I-NEXT: +// CHECK-I-NEXT: + +union A { int X; int Y; }; +// CHECK-A: + // CHECK-A-NEXT: + // CHECK-A-NEXT: blob data = 'A' + // CHECK-A-NEXT: blob data = '{{.*}}' + // CHECK-A-NEXT: + // CHECK-A-NEXT: + // CHECK-A-NEXT: + // CHECK-A-NEXT: blob data = 'int' + // CHECK-A-NEXT: + // CHECK-A-NEXT: + // CHECK-A-NEXT: blob data = 'X' + // CHECK-A-NEXT: + // CHECK-A-NEXT: + // CHECK-A-NEXT: + // CHECK-A-NEXT: + // CHECK-A-NEXT: blob data = 'int' + // CHECK-A-NEXT: + // CHECK-A-NEXT: + // CHECK-A-NEXT: blob data = 'Y' + // CHECK-A-NEXT: + // CHECK-A-NEXT: +// CHECK-A-NEXT: + +enum B { X, Y }; +// CHECK-B: + // CHECK-B-NEXT: + // CHECK-B-NEXT: blob data = 'B' + // CHECK-B-NEXT: blob data = '{{.*}}' + // CHECK-B-NEXT: blob data = 'X' + // CHECK-B-NEXT: blob data = 'Y' +// CHECK-B-NEXT: + +enum class Bc { A, B }; +// CHECK-BC: + // CHECK-BC-NEXT: + // CHECK-BC-NEXT: blob data = 'Bc' + // CHECK-BC-NEXT: blob data = '{{.*}}' + // CHECK-BC-NEXT: + // CHECK-BC-NEXT: blob data = 'A' + // CHECK-BC-NEXT: blob data = 'B' +// CHECK-BC-NEXT: + +struct C { int i; }; +// CHECK-C: + // CHECK-C-NEXT: + // CHECK-C-NEXT: blob data = 'C' + // CHECK-C-NEXT: blob data = '{{.*}}' + // CHECK-C-NEXT: + // CHECK-C-NEXT: + // CHECK-C-NEXT: blob data = 'int' + // CHECK-C-NEXT: + // CHECK-C-NEXT: + // CHECK-C-NEXT: blob data = 'i' + // CHECK-C-NEXT: + // CHECK-C-NEXT: +// CHECK-C-NEXT: + +class D {}; +// CHECK-D: + // CHECK-D-NEXT: + // CHECK-D-NEXT: blob data = 'D' + // CHECK-D-NEXT: blob data = '{{.*}}' + // CHECK-D-NEXT: +// CHECK-D-NEXT: + +class E { +public: + E() {} + ~E() {} + +protected: + void ProtectedMethod(); +}; +// CHECK-E: + // CHECK-E-NEXT: + // CHECK-E-NEXT: blob data = 'E' + // CHECK-E-NEXT: blob data = '{{.*}}' + // CHECK-E-NEXT: +// CHECK-E-NEXT: + +// CHECK-ECON: + // CHECK-ECON-NEXT: + // CHECK-ECON-NEXT: blob data = 'E' + // CHECK-ECON-NEXT: + // CHECK-ECON-NEXT: + // CHECK-ECON-NEXT: blob data = 'E' + // CHECK-ECON-NEXT: + // CHECK-ECON-NEXT: + // CHECK-ECON-NEXT: + // CHECK-ECON-NEXT: + // CHECK-ECON-NEXT: blob data = '{{.*}}' + // CHECK-ECON-NEXT: + // CHECK-ECON-NEXT: + // CHECK-ECON-NEXT: blob data = 'E' + // CHECK-ECON-NEXT: + // CHECK-ECON-NEXT: + // CHECK-ECON-NEXT: + // CHECK-ECON-NEXT: + // CHECK-ECON-NEXT: + // CHECK-ECON-NEXT: blob data = 'void' + // CHECK-ECON-NEXT: + // CHECK-ECON-NEXT: + // CHECK-ECON-NEXT: +// CHECK-ECON-NEXT: + +// CHECK-EDES: + // CHECK-EDES-NEXT: + // CHECK-EDES-NEXT: blob data = '~E' + // CHECK-EDES-NEXT: + // CHECK-EDES-NEXT: + // CHECK-EDES-NEXT: blob data = 'E' + // CHECK-EDES-NEXT: + // CHECK-EDES-NEXT: + // CHECK-EDES-NEXT: + // CHECK-EDES-NEXT: + // CHECK-EDES-NEXT: blob data = '{{.*}}' + // CHECK-EDES-NEXT: + // CHECK-EDES-NEXT: + // CHECK-EDES-NEXT: blob data = 'E' + // CHECK-EDES-NEXT: + // CHECK-EDES-NEXT: + // CHECK-EDES-NEXT: + // CHECK-EDES-NEXT: + // CHECK-EDES-NEXT: + // CHECK-EDES-NEXT: blob data = 'void' + // CHECK-EDES-NEXT: + // CHECK-EDES-NEXT: + // CHECK-EDES-NEXT: +// CHECK-EDES-NEXT: + +void E::ProtectedMethod() {} +// CHECK-PM: + // CHECK-PM-NEXT: + // CHECK-PM-NEXT: blob data = 'ProtectedMethod' + // CHECK-PM-NEXT: + // CHECK-PM-NEXT: + // CHECK-PM-NEXT: blob data = 'E' + // CHECK-PM-NEXT: + // CHECK-PM-NEXT: + // CHECK-PM-NEXT: + // CHECK-PM-NEXT: + // CHECK-PM-NEXT: blob data = '{{.*}}' + // CHECK-PM-NEXT: blob data = '{{.*}}' + // CHECK-PM-NEXT: + // CHECK-PM-NEXT: + // CHECK-PM-NEXT: blob data = 'E' + // CHECK-PM-NEXT: + // CHECK-PM-NEXT: + // CHECK-PM-NEXT: + // CHECK-PM-NEXT: + // CHECK-PM-NEXT: + // CHECK-PM-NEXT: blob data = 'void' + // CHECK-PM-NEXT: + // CHECK-PM-NEXT: + // CHECK-PM-NEXT: +// CHECK-PM-NEXT: + + + +class F : virtual private D, public E {}; +// CHECK-F: + // CHECK-F-NEXT: + // CHECK-F-NEXT: blob data = 'F' + // CHECK-F-NEXT: blob data = '{{.*}}' + // CHECK-F-NEXT: + // CHECK-F-NEXT: + // CHECK-F-NEXT: + // CHECK-F-NEXT: blob data = 'E' + // CHECK-F-NEXT: + // CHECK-F-NEXT: + // CHECK-F-NEXT: + // CHECK-F-NEXT: + // CHECK-F-NEXT: + // CHECK-F-NEXT: blob data = 'D' + // CHECK-F-NEXT: + // CHECK-F-NEXT: + // CHECK-F-NEXT: +// CHECK-F-NEXT: + +class X { + class Y {}; +}; +// CHECK-X: + // CHECK-X-NEXT: + // CHECK-X-NEXT: blob data = 'X' + // CHECK-X-NEXT: blob data = '{{.*}}' + // CHECK-X-NEXT: +// CHECK-X-NEXT: + +// CHECK-Y: + // CHECK-Y-NEXT: + // CHECK-Y-NEXT: blob data = 'Y' + // CHECK-Y-NEXT: + // CHECK-Y-NEXT: + // CHECK-Y-NEXT: blob data = 'X' + // CHECK-Y-NEXT: + // CHECK-Y-NEXT: + // CHECK-Y-NEXT: + // CHECK-Y-NEXT: blob data = '{{.*}}' + // CHECK-Y-NEXT: +// CHECK-Y-NEXT: