r179907 - [libclang] Make sure the preable does not truncate comments.
Argyrios Kyrtzidis
akyrtzi at gmail.com
Fri Apr 19 16:24:26 PDT 2013
Author: akirtzidis
Date: Fri Apr 19 18:24:25 2013
New Revision: 179907
URL: http://llvm.org/viewvc/llvm-project?rev=179907&view=rev
Log:
[libclang] Make sure the preable does not truncate comments.
rdar://13647445
Added:
cfe/trunk/test/Index/comment-with-preamble.c
Modified:
cfe/trunk/lib/Lex/Lexer.cpp
Modified: cfe/trunk/lib/Lex/Lexer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/Lexer.cpp?rev=179907&r1=179906&r2=179907&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/Lexer.cpp (original)
+++ cfe/trunk/lib/Lex/Lexer.cpp Fri Apr 19 18:24:25 2013
@@ -557,6 +557,7 @@ Lexer::ComputePreamble(const llvm::Memor
SourceLocation FileLoc = SourceLocation::getFromRawEncoding(StartOffset);
Lexer TheLexer(FileLoc, LangOpts, Buffer->getBufferStart(),
Buffer->getBufferStart(), Buffer->getBufferEnd());
+ TheLexer.SetCommentRetentionState(true);
// StartLoc will differ from FileLoc if there is a BOM that was skipped.
SourceLocation StartLoc = TheLexer.getSourceLocation();
@@ -565,6 +566,7 @@ Lexer::ComputePreamble(const llvm::Memor
Token TheTok;
Token IfStartTok;
unsigned IfCount = 0;
+ SourceLocation ActiveCommentLoc;
unsigned MaxLineOffset = 0;
if (MaxLines) {
@@ -612,13 +614,17 @@ Lexer::ComputePreamble(const llvm::Memor
}
// Comments are okay; skip over them.
- if (TheTok.getKind() == tok::comment)
+ if (TheTok.getKind() == tok::comment) {
+ if (ActiveCommentLoc.isInvalid())
+ ActiveCommentLoc = TheTok.getLocation();
continue;
+ }
if (TheTok.isAtStartOfLine() && TheTok.getKind() == tok::hash) {
// This is the start of a preprocessor directive.
Token HashTok = TheTok;
InPreprocessorDirective = true;
+ ActiveCommentLoc = SourceLocation();
// Figure out which directive this is. Since we're lexing raw tokens,
// we don't have an identifier table available. Instead, just look at
@@ -689,7 +695,14 @@ Lexer::ComputePreamble(const llvm::Memor
break;
} while (true);
- SourceLocation End = IfCount? IfStartTok.getLocation() : TheTok.getLocation();
+ SourceLocation End;
+ if (IfCount)
+ End = IfStartTok.getLocation();
+ else if (ActiveCommentLoc.isValid())
+ End = ActiveCommentLoc; // don't truncate a decl comment.
+ else
+ End = TheTok.getLocation();
+
return std::make_pair(End.getRawEncoding() - StartLoc.getRawEncoding(),
IfCount? IfStartTok.isAtStartOfLine()
: TheTok.isAtStartOfLine());
Added: cfe/trunk/test/Index/comment-with-preamble.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/comment-with-preamble.c?rev=179907&view=auto
==============================================================================
--- cfe/trunk/test/Index/comment-with-preamble.c (added)
+++ cfe/trunk/test/Index/comment-with-preamble.c Fri Apr 19 18:24:25 2013
@@ -0,0 +1,13 @@
+// Make sure the preable does not truncate comments.
+
+#ifndef BAZ
+#define BAZ 3
+#endif
+
+//! Fooâs description.
+void Foo();
+
+// RUN: c-index-test -test-load-source-reparse 1 local %s | FileCheck %s
+// RUN: env CINDEXTEST_EDITING=1 c-index-test -test-load-source-reparse 1 local %s | FileCheck %s
+
+// CHECK: FunctionDecl=Foo:8:6 RawComment=[//! Fooâs description.] RawCommentRange=[7:1 - 7:25] BriefComment=[Fooâs description.]
More information about the cfe-commits
mailing list