File tree Expand file tree Collapse file tree 3 files changed +18
-3
lines changed Expand file tree Collapse file tree 3 files changed +18
-3
lines changed Original file line number Diff line number Diff line change 12
12
#include " clang/AST/RawCommentList.h"
13
13
#include " clang/Basic/SourceManager.h"
14
14
#include " clang/Sema/CodeCompleteConsumer.h"
15
+ #include " llvm/Support/JSON.h"
15
16
#include < limits>
16
17
#include < utility>
17
18
@@ -86,7 +87,12 @@ std::string getDeclComment(const ASTContext &Ctx, const NamedDecl &Decl) {
86
87
assert (!Ctx.getSourceManager ().isLoadedSourceLocation (RC->getBeginLoc ()));
87
88
std::string Doc =
88
89
RC->getFormattedText (Ctx.getSourceManager (), Ctx.getDiagnostics ());
89
- return looksLikeDocComment (Doc) ? Doc : " " ;
90
+ if (!looksLikeDocComment (Doc))
91
+ return " " ;
92
+ // Clang requires source to be UTF-8, but doesn't enforce this in comments.
93
+ if (!llvm::json::isUTF8 (Doc))
94
+ Doc = llvm::json::fixUTF8 (Doc);
95
+ return Doc;
90
96
}
91
97
92
98
void getSignature (const CodeCompletionString &CCS, std::string *Signature,
Original file line number Diff line number Diff line change 7
7
// ===----------------------------------------------------------------------===//
8
8
9
9
#include " CodeCompletionStrings.h"
10
+ #include " TestTU.h"
10
11
#include " clang/Sema/CodeCompleteConsumer.h"
11
12
#include " gmock/gmock.h"
12
13
#include " gtest/gtest.h"
@@ -56,6 +57,14 @@ TEST_F(CompletionStringTest, DocumentationWithAnnotation) {
56
57
" Annotation: Ano\n\n Is this brief?" );
57
58
}
58
59
60
+ TEST_F (CompletionStringTest, GetDeclCommentBadUTF8) {
61
+ // <ff> is not a valid byte here, should be replaced by encoded <U+FFFD>.
62
+ auto TU = TestTU::withCode (" /*x\xff y*/ struct X;" );
63
+ auto AST = TU.build ();
64
+ EXPECT_EQ (" x\xef\xbf\xbd y" ,
65
+ getDeclComment (AST.getASTContext (), findDecl (AST, " X" )));
66
+ }
67
+
59
68
TEST_F (CompletionStringTest, MultipleAnnotations) {
60
69
Builder.AddAnnotation (" Ano1" );
61
70
Builder.AddAnnotation (" Ano2" );
Original file line number Diff line number Diff line change @@ -1606,11 +1606,11 @@ TEST_F(SymbolCollectorTest, BadUTF8) {
1606
1606
// Extracted from boost/spirit/home/support/char_encoding/iso8859_1.hpp
1607
1607
// This looks like UTF-8 and fools clang, but has high-ISO-8859-1 comments.
1608
1608
const char *Header = " int PUNCT = 0;\n "
1609
- " int types[] = { /* \xa1 */PUNCT };" ;
1609
+ " /* \xa1 */ int types[] = { /* \xa1 */PUNCT };" ;
1610
1610
CollectorOpts.RefFilter = RefKind::All;
1611
1611
CollectorOpts.RefsInHeaders = true ;
1612
1612
runSymbolCollector (Header, " " );
1613
- EXPECT_THAT (Symbols, Contains (QName (" types" )));
1613
+ EXPECT_THAT (Symbols, Contains (AllOf ( QName (" types" ), Doc ( " \xef\xbf\xbd " ) )));
1614
1614
EXPECT_THAT (Symbols, Contains (QName (" PUNCT" )));
1615
1615
// Reference is stored, although offset within line is not reliable.
1616
1616
EXPECT_THAT (Refs, Contains (Pair (findSymbol (Symbols, " PUNCT" ).ID , _)));
You can’t perform that action at this time.
0 commit comments