Comment parsing: attaching comments to declarations

We can attach comments to declarations.  Right now we only support comments
that precede the declarations (trailing comments will be supported later).

The implementation approach is different from one we have in Clang.  In Swift
the Lexer attaches the comments to the next token, and parser checks if
comments are present on the first token of the declaration.  This is much
cleaner, and faster than Clang's approach (where we perform a binary search on
source locations and do ad-hoc fixups afterwards).

The comment <-> decl correspondence is modeled as "virtual" attributes that can
not be spelled in the source.  These attributes are not serialized at the
moment -- this will be implemented later.


Swift SVN r14031
This commit is contained in:
Dmitri Hrybenko
2014-02-18 09:04:37 +00:00
parent 61f7a7c482
commit ecd798b9fd
13 changed files with 488 additions and 78 deletions

View File

@@ -63,6 +63,15 @@ unsigned SourceManager::getByteDistance(SourceLoc Start, SourceLoc End) const {
return End.Value.getPointer() - Start.Value.getPointer();
}
StringRef SourceManager::extractText(CharSourceRange Range) const {
assert(Range.isValid() && "range should be valid");
unsigned BufferID = findBufferContainingLoc(Range.getStart());
StringRef Buffer = LLVMSourceMgr.getMemoryBuffer(BufferID)->getBuffer();
return Buffer.substr(getLocOffsetInBuffer(Range.getStart(), BufferID),
Range.getByteLength());
}
void SourceLoc::printLineAndColumn(raw_ostream &OS,
const SourceManager &SM) const {
if (isInvalid()) {