Skip to content

Commit 22aeeb1

Browse files
Fix ClangImport crash on Windows paths (#4246)
* Fix crash on Windows paths * Fix test * Enable fix * Cut trailing ':'
1 parent b170a9c commit 22aeeb1

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

lib/clangimport.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -497,17 +497,21 @@ void clangimport::AstNode::dumpAst(int num, int indent) const
497497
void clangimport::AstNode::setLocations(TokenList *tokenList, int file, int line, int col)
498498
{
499499
for (const std::string &ext: mExtTokens) {
500-
if (ext.compare(0,5,"<col:") == 0)
500+
if (ext.compare(0, 5, "<col:") == 0)
501501
col = std::atoi(ext.substr(5).c_str());
502-
else if (ext.compare(0,6,"<line:") == 0) {
502+
else if (ext.compare(0, 6, "<line:") == 0) {
503503
line = std::atoi(ext.substr(6).c_str());
504504
if (ext.find(", col:") != std::string::npos)
505505
col = std::atoi(ext.c_str() + ext.find(", col:") + 6);
506-
} else if (ext[0] == '<' && ext.find(":") != std::string::npos) {
507-
std::string::size_type sep1 = ext.find(":");
508-
std::string::size_type sep2 = ext.find(":", sep1+1);
509-
file = tokenList->appendFileIfNew(ext.substr(1, sep1 - 1));
510-
line = MathLib::toLongNumber(ext.substr(sep1+1, sep2-sep1));
506+
} else if (ext[0] == '<') {
507+
const std::string::size_type colon = ext.find(':');
508+
if (colon != std::string::npos) {
509+
const bool windowsPath = colon == 2 && ext.size() > 4 && ext[3] == '\\';
510+
std::string::size_type sep1 = windowsPath ? ext.find(':', 4) : colon;
511+
std::string::size_type sep2 = ext.find(':', sep1 + 1);
512+
file = tokenList->appendFileIfNew(ext.substr(1, sep1 - 1));
513+
line = MathLib::toLongNumber(ext.substr(sep1 + 1, sep2 - sep1 - 1));
514+
}
511515
}
512516
}
513517
mFile = file;

test/testclangimport.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,10 @@ class TestClangImport : public TestFixture {
584584
}
585585

586586
void cxxRecordDecl1() {
587-
const char clang[] = "`-CXXRecordDecl 0x34cc5f8 <1.cpp:2:1, col:7> col:7 class Foo";
587+
const char* clang = "`-CXXRecordDecl 0x34cc5f8 <1.cpp:2:1, col:7> col:7 class Foo";
588+
ASSERT_EQUALS("class Foo ;", parse(clang));
589+
590+
clang = "`-CXXRecordDecl 0x34cc5f8 <C:\\Foo\\Bar Baz\\1.cpp:2:1, col:7> col:7 class Foo";
588591
ASSERT_EQUALS("class Foo ;", parse(clang));
589592
}
590593

0 commit comments

Comments
 (0)