diff --git a/.vscode/launch.json b/.vscode/launch.json index 16bfe4bd..a148c8c4 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -19,8 +19,8 @@ { "type": "lldb", "request": "launch", - "name": "clice_test", - "program": "${workspaceFolder}/build/bin/clice_test", + "name": "PCM", + "program": "${workspaceFolder}/build/bin/clice-tests", "args": [ "--test-dir=/home/ykiko/C++/clice2/tests", "--gtest_filter=clice.PCM" @@ -31,7 +31,7 @@ "type": "lldb", "request": "launch", "name": "TemplateResolver", - "program": "${workspaceFolder}/build/bin/clice_test", + "program": "${workspaceFolder}/build/bin/clice-tests", "args": [ "--test-dir=/home/ykiko/C++/clice2/tests", "--gtest_filter=clice.TemplateResolver" @@ -42,7 +42,7 @@ "type": "lldb", "request": "launch", "name": "Index", - "program": "${workspaceFolder}/build/bin/clice_test", + "program": "${workspaceFolder}/build/bin/clice-tests", "args": [ "--test-dir=/home/ykiko/C++/clice2/tests", "--gtest_filter=clice.Index" diff --git a/src/Index/Index.cpp b/src/Index/Index.cpp index 2d72e4ca..27345ce4 100644 --- a/src/Index/Index.cpp +++ b/src/Index/Index.cpp @@ -92,8 +92,8 @@ public: } VISIT_DECL(VarDecl) { - llvm::outs() << "-------------------------\n"; - decl->getTypeSourceInfo()->getTypeLoc().dump(); + // llvm::outs() << "-------------------------\n"; + // decl->getTypeSourceInfo()->getTypeLoc().dump(); return true; } diff --git a/src/Index/Indexer.cpp b/src/Index/Indexer.cpp index fb13f64b..0568a815 100644 --- a/src/Index/Indexer.cpp +++ b/src/Index/Indexer.cpp @@ -5,13 +5,32 @@ namespace clice { namespace { -Location toLocation(clang::SourceRange loc, clang::SourceManager& srcMgr) { - Location location; - location.uri = srcMgr.getFilename(loc.getBegin()); - location.range.start.line = srcMgr.getPresumedLineNumber(loc.getBegin()); - location.range.start.character = srcMgr.getPresumedColumnNumber(loc.getBegin()); - location.range.end.line = srcMgr.getPresumedLineNumber(loc.getEnd()); - location.range.end.character = srcMgr.getPresumedColumnNumber(loc.getEnd()); +Location toRange(clang::SourceRange range, clang::syntax::TokenBuffer& tokBuf) { + auto& srcMgr = tokBuf.sourceManager(); + Location location{}; + location.uri = srcMgr.getFilename(range.getBegin()); + /// It's impossible that a range has crossed multiple files. + assert(location.uri == srcMgr.getFilename(range.getEnd())); + + if(range.getBegin().isMacroID() || range.getEnd().isMacroID()) { + range.dump(srcMgr); + return location; + } + + auto begin = tokBuf.spelledTokenContaining(range.getBegin()); + auto end = tokBuf.spelledTokenContaining(range.getEnd()); + + if(!begin || !end) { + // FIXME: + std::terminate(); + } + + location.range.start.line = srcMgr.getPresumedLineNumber(begin->location()); + location.range.start.character = srcMgr.getPresumedColumnNumber(begin->location()); + + location.range.end.line = srcMgr.getPresumedLineNumber(end->endLocation()); + location.range.end.character = srcMgr.getPresumedColumnNumber(end->endLocation()); + return location; } @@ -54,7 +73,7 @@ Indexer& Indexer::addOccurrence(const clang::NamedDecl* decl, clang::SourceRange auto ID = symbols[lookup(decl)].ID; auto& srcMgr = sema.getSourceManager(); - occurrences.emplace_back(Occurrence{ID, toLocation(range.getBegin(), srcMgr)}); + occurrences.emplace_back(Occurrence{ID, toRange(range, tokBuf)}); return *this; } @@ -64,7 +83,7 @@ Indexer& Indexer::addOccurrence(int Kind, clang::SourceLocation loc) { } auto ID = SymbolID::fromKind(Kind); - occurrences.emplace_back(Occurrence{ID, toLocation(loc, sema.getSourceManager())}); + occurrences.emplace_back(Occurrence{ID, toRange(loc, tokBuf)}); return *this; } @@ -78,7 +97,7 @@ Indexer& Indexer::addRelation(const clang::NamedDecl* from, auto index = lookup(from); auto& relations = this->relations[index]; for(auto role: roles) { - relations.emplace_back(Relation{role, toLocation(range, sema.getSourceManager())}); + relations.emplace_back(Relation{role, toRange(range, tokBuf)}); } return *this; } diff --git a/tests/Index/Vector.cpp b/tests/Index/Vector.cpp new file mode 100644 index 00000000..03bc5668 --- /dev/null +++ b/tests/Index/Vector.cpp @@ -0,0 +1 @@ +#include \ No newline at end of file diff --git a/unittests/Index/Index.cpp b/unittests/Index/Index.cpp index 615beca9..7b40fafb 100644 --- a/unittests/Index/Index.cpp +++ b/unittests/Index/Index.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -24,6 +25,16 @@ TEST(clice, Index) { std::error_code EC; llvm::raw_fd_ostream fileStream("output.json", EC); fileStream << value << "\n"; - llvm::outs() << value << "\n"; + + llvm::outs() << "Index symbol count: " << csif.symbols.size() << "\n"; + // llvm::outs() << value << "\n"; + + if(filepath.ends_with("ClassTemplate.cpp")) { + Loader loader(csif, nullptr); + Location location; + location.range = {14, 1, 14, 2}; + auto& sym = loader.locate(location); + llvm::outs() << sym.document << "\n"; + } }); }