From 48f1a006b78944a944cdc0c98fb4b447e19fce7d Mon Sep 17 00:00:00 2001 From: Ka Ho Ng Date: Fri, 6 Dec 2024 20:58:19 -0500 Subject: [PATCH] Reformat all the files after 192a82b (#979) Since the introduction of "ColumnLimit: 120" in .clang-format, the column limit has become 120 characters instead of 80 characters. This prevents clang-format from generating too much changes even if just a small portion of a source file or header file is modified. --- src/clang_tu.cc | 32 ++- src/clang_tu.hh | 19 +- src/config.hh | 44 ++-- src/filesystem.cc | 9 +- src/filesystem.hh | 3 +- src/fuzzy_match.cc | 14 +- src/hierarchy.hh | 3 +- src/indexer.cc | 234 +++++++-------------- src/indexer.hh | 83 +++----- src/log.cc | 6 +- src/log.hh | 13 +- src/lsp.cc | 15 +- src/lsp.hh | 47 ++--- src/main.cc | 39 ++-- src/message_handler.cc | 61 ++---- src/message_handler.hh | 45 ++-- src/messages/ccls_call.cc | 74 ++----- src/messages/ccls_info.cc | 6 +- src/messages/ccls_inheritance.cc | 32 +-- src/messages/ccls_member.cc | 48 ++--- src/messages/ccls_navigate.cc | 25 +-- src/messages/ccls_vars.cc | 3 +- src/messages/initialize.cc | 53 ++--- src/messages/textDocument_code.cc | 46 ++-- src/messages/textDocument_completion.cc | 155 ++++++-------- src/messages/textDocument_definition.cc | 36 ++-- src/messages/textDocument_did.cc | 6 +- src/messages/textDocument_document.cc | 74 +++---- src/messages/textDocument_foldingRange.cc | 9 +- src/messages/textDocument_formatting.cc | 35 ++- src/messages/textDocument_hover.cc | 16 +- src/messages/textDocument_references.cc | 11 +- src/messages/textDocument_rename.cc | 9 +- src/messages/textDocument_signatureHelp.cc | 41 ++-- src/messages/workspace.cc | 38 ++-- src/pipeline.cc | 136 +++++------- src/pipeline.hh | 13 +- src/platform_win.cc | 7 +- src/position.cc | 7 +- src/position.hh | 12 +- src/project.cc | 69 ++---- src/project.hh | 3 +- src/query.cc | 216 ++++++++----------- src/query.hh | 50 ++--- src/sema_manager.cc | 230 +++++++------------- src/sema_manager.hh | 38 ++-- src/serializer.cc | 53 ++--- src/serializer.hh | 105 ++++----- src/test.cc | 210 ++++++++---------- src/threaded_queue.hh | 21 +- src/utils.cc | 31 +-- src/utils.hh | 40 ++-- src/working_files.cc | 76 +++---- src/working_files.hh | 9 +- 54 files changed, 962 insertions(+), 1748 deletions(-) diff --git a/src/clang_tu.cc b/src/clang_tu.cc index 54e04ef0..5c880b32 100644 --- a/src/clang_tu.cc +++ b/src/clang_tu.cc @@ -57,10 +57,8 @@ bool isInsideMainFile(const SourceManager &sm, SourceLocation sl) { return fid == sm.getMainFileID() || fid == sm.getPreambleFileID(); } -static Pos decomposed2LineAndCol(const SourceManager &sm, - std::pair i) { - int l = (int)sm.getLineNumber(i.first, i.second) - 1, - c = (int)sm.getColumnNumber(i.first, i.second) - 1; +static Pos decomposed2LineAndCol(const SourceManager &sm, std::pair i) { + int l = (int)sm.getLineNumber(i.first, i.second) - 1, c = (int)sm.getColumnNumber(i.first, i.second) - 1; bool invalid = false; StringRef buf = sm.getBufferData(i.first, &invalid); if (!invalid) { @@ -71,15 +69,12 @@ static Pos decomposed2LineAndCol(const SourceManager &sm, while (i < p.size() && (uint8_t)p[i] >= 128 && (uint8_t)p[i] < 192) i++; } - return {(uint16_t)std::min(l, UINT16_MAX), - (int16_t)std::min(c, INT16_MAX)}; + return {(uint16_t)std::min(l, UINT16_MAX), (int16_t)std::min(c, INT16_MAX)}; } -Range fromCharSourceRange(const SourceManager &sm, const LangOptions &lang, - CharSourceRange csr, FileID *fid) { +Range fromCharSourceRange(const SourceManager &sm, const LangOptions &lang, CharSourceRange csr, FileID *fid) { SourceLocation bloc = csr.getBegin(), eloc = csr.getEnd(); - std::pair binfo = sm.getDecomposedLoc(bloc), - einfo = sm.getDecomposedLoc(eloc); + std::pair binfo = sm.getDecomposedLoc(bloc), einfo = sm.getDecomposedLoc(eloc); if (csr.isTokenRange()) einfo.second += Lexer::MeasureTokenLength(eloc, sm, lang); if (fid) @@ -87,13 +82,12 @@ Range fromCharSourceRange(const SourceManager &sm, const LangOptions &lang, return {decomposed2LineAndCol(sm, binfo), decomposed2LineAndCol(sm, einfo)}; } -Range fromTokenRange(const SourceManager &sm, const LangOptions &lang, - SourceRange sr, FileID *fid) { +Range fromTokenRange(const SourceManager &sm, const LangOptions &lang, SourceRange sr, FileID *fid) { return fromCharSourceRange(sm, lang, CharSourceRange::getTokenRange(sr), fid); } -Range fromTokenRangeDefaulted(const SourceManager &sm, const LangOptions &lang, - SourceRange sr, FileID fid, Range range) { +Range fromTokenRangeDefaulted(const SourceManager &sm, const LangOptions &lang, SourceRange sr, FileID fid, + Range range) { auto decomposed = sm.getDecomposedLoc(sm.getExpansionLoc(sr.getBegin())); if (decomposed.first == fid) range.start = decomposed2LineAndCol(sm, decomposed); @@ -106,17 +100,15 @@ Range fromTokenRangeDefaulted(const SourceManager &sm, const LangOptions &lang, return range; } -std::unique_ptr -buildCompilerInvocation(const std::string &main, std::vector args, - IntrusiveRefCntPtr vfs) { +std::unique_ptr buildCompilerInvocation(const std::string &main, std::vector args, + IntrusiveRefCntPtr vfs) { std::string save = "-resource-dir=" + g_config->clang.resourceDir; args.push_back(save.c_str()); args.push_back("-fsyntax-only"); // Similar to clang/tools/driver/driver.cpp:insertTargetAndModeArgs but don't // require llvm::InitializeAllTargetInfos(). - auto target_and_mode = - driver::ToolChain::getTargetAndModeFromProgramName(args[0]); + auto target_and_mode = driver::ToolChain::getTargetAndModeFromProgramName(args[0]); if (target_and_mode.DriverMode) args.insert(args.begin() + 1, target_and_mode.DriverMode); if (!target_and_mode.TargetPrefix.empty()) { @@ -145,7 +137,7 @@ buildCompilerInvocation(const std::string &main, std::vector args, const driver::JobList &jobs = comp->getJobs(); bool offload_compilation = false; if (jobs.size() > 1) { - for (auto &a : comp->getActions()){ + for (auto &a : comp->getActions()) { // On MacOSX real actions may end up being wrapped in BindArchAction if (isa(a)) a = *a->input_begin(); diff --git a/src/clang_tu.hh b/src/clang_tu.hh index fd98ced1..b786c169 100644 --- a/src/clang_tu.hh +++ b/src/clang_tu.hh @@ -28,23 +28,18 @@ std::string pathFromFileEntry(clang::FileEntryRef file); bool isInsideMainFile(const clang::SourceManager &sm, clang::SourceLocation sl); -Range fromCharSourceRange(const clang::SourceManager &sm, - const clang::LangOptions &lang, - clang::CharSourceRange csr, +Range fromCharSourceRange(const clang::SourceManager &sm, const clang::LangOptions &lang, clang::CharSourceRange csr, clang::FileID *fid = nullptr); -Range fromTokenRange(const clang::SourceManager &sm, - const clang::LangOptions &lang, clang::SourceRange sr, +Range fromTokenRange(const clang::SourceManager &sm, const clang::LangOptions &lang, clang::SourceRange sr, clang::FileID *fid = nullptr); -Range fromTokenRangeDefaulted(const clang::SourceManager &sm, - const clang::LangOptions &lang, - clang::SourceRange sr, clang::FileID fid, - Range range); +Range fromTokenRangeDefaulted(const clang::SourceManager &sm, const clang::LangOptions &lang, clang::SourceRange sr, + clang::FileID fid, Range range); -std::unique_ptr -buildCompilerInvocation(const std::string &main, std::vector args, - llvm::IntrusiveRefCntPtr VFS); +std::unique_ptr buildCompilerInvocation(const std::string &main, + std::vector args, + llvm::IntrusiveRefCntPtr VFS); const char *clangBuiltinTypeName(int); } // namespace ccls diff --git a/src/config.hh b/src/config.hh index b768e476..0faeadaf 100644 --- a/src/config.hh +++ b/src/config.hh @@ -324,42 +324,30 @@ struct Config { int maxNum = 2000; } xref; }; -REFLECT_STRUCT(Config::Cache, directory, format, hierarchicalPath, - retainInMemory); -REFLECT_STRUCT(Config::ServerCap::DocumentOnTypeFormattingOptions, - firstTriggerCharacter, moreTriggerCharacter); -REFLECT_STRUCT(Config::ServerCap::Workspace::WorkspaceFolders, supported, - changeNotifications); +REFLECT_STRUCT(Config::Cache, directory, format, hierarchicalPath, retainInMemory); +REFLECT_STRUCT(Config::ServerCap::DocumentOnTypeFormattingOptions, firstTriggerCharacter, moreTriggerCharacter); +REFLECT_STRUCT(Config::ServerCap::Workspace::WorkspaceFolders, supported, changeNotifications); REFLECT_STRUCT(Config::ServerCap::Workspace, workspaceFolders); -REFLECT_STRUCT(Config::ServerCap, documentOnTypeFormattingProvider, - foldingRangeProvider, workspace); -REFLECT_STRUCT(Config::Clang, excludeArgs, extraArgs, pathMappings, - resourceDir); -REFLECT_STRUCT(Config::ClientCapability, diagnosticsRelatedInformation, - hierarchicalDocumentSymbolSupport, linkSupport, snippetSupport); +REFLECT_STRUCT(Config::ServerCap, documentOnTypeFormattingProvider, foldingRangeProvider, workspace); +REFLECT_STRUCT(Config::Clang, excludeArgs, extraArgs, pathMappings, resourceDir); +REFLECT_STRUCT(Config::ClientCapability, diagnosticsRelatedInformation, hierarchicalDocumentSymbolSupport, linkSupport, + snippetSupport); REFLECT_STRUCT(Config::CodeLens, localVariables); -REFLECT_STRUCT(Config::Completion::Include, blacklist, maxPathSize, - suffixWhitelist, whitelist); -REFLECT_STRUCT(Config::Completion, caseSensitivity, detailedLabel, - dropOldRequests, duplicateOptional, filterAndSort, include, - maxNum, placeholder); -REFLECT_STRUCT(Config::Diagnostics, blacklist, onChange, onOpen, onSave, - spellChecking, whitelist) +REFLECT_STRUCT(Config::Completion::Include, blacklist, maxPathSize, suffixWhitelist, whitelist); +REFLECT_STRUCT(Config::Completion, caseSensitivity, detailedLabel, dropOldRequests, duplicateOptional, filterAndSort, + include, maxNum, placeholder); +REFLECT_STRUCT(Config::Diagnostics, blacklist, onChange, onOpen, onSave, spellChecking, whitelist) REFLECT_STRUCT(Config::Highlight, largeFileSize, rainbow, blacklist, whitelist) REFLECT_STRUCT(Config::Index::Name, suppressUnwrittenScope); -REFLECT_STRUCT(Config::Index, blacklist, comments, initialNoLinkage, - initialBlacklist, initialWhitelist, maxInitializerLines, - multiVersion, multiVersionBlacklist, multiVersionWhitelist, name, - onChange, parametersInDeclarations, threads, trackDependency, - whitelist); +REFLECT_STRUCT(Config::Index, blacklist, comments, initialNoLinkage, initialBlacklist, initialWhitelist, + maxInitializerLines, multiVersion, multiVersionBlacklist, multiVersionWhitelist, name, onChange, + parametersInDeclarations, threads, trackDependency, whitelist); REFLECT_STRUCT(Config::Request, timeout); REFLECT_STRUCT(Config::Session, maxNum); REFLECT_STRUCT(Config::WorkspaceSymbol, caseSensitivity, maxNum, sort); REFLECT_STRUCT(Config::Xref, maxNum); -REFLECT_STRUCT(Config, compilationDatabaseCommand, compilationDatabaseDirectory, - cache, capabilities, clang, client, codeLens, completion, - diagnostics, highlight, index, request, session, workspaceSymbol, - xref); +REFLECT_STRUCT(Config, compilationDatabaseCommand, compilationDatabaseDirectory, cache, capabilities, clang, client, + codeLens, completion, diagnostics, highlight, index, request, session, workspaceSymbol, xref); extern Config *g_config; diff --git a/src/filesystem.cc b/src/filesystem.cc index dad07ddd..3e788284 100644 --- a/src/filesystem.cc +++ b/src/filesystem.cc @@ -29,12 +29,10 @@ void getFilesInFolder(std::string folder, bool recursive, bool dir_prefix, std::error_code ec; std::string folder1 = curr.back(); curr.pop_back(); - for (sys::fs::directory_iterator i(folder1, ec, false), e; i != e && !ec; - i.increment(ec)) { + for (sys::fs::directory_iterator i(folder1, ec, false), e; i != e && !ec; i.increment(ec)) { std::string path = i->path(); std::string filename(sys::path::filename(path)); - if ((filename[0] == '.' && filename != ".ccls") || - sys::fs::status(path, status, false)) + if ((filename[0] == '.' && filename != ".ccls") || sys::fs::status(path, status, false)) continue; if (sys::fs::is_symlink_file(status)) { if (sys::fs::status(path, status, true)) @@ -49,8 +47,7 @@ void getFilesInFolder(std::string folder, bool recursive, bool dir_prefix, if (!dir_prefix) path = path.substr(folder.size()); handler(sys::path::convert_to_slash(path)); - } else if (recursive && sys::fs::is_directory(status) && - !seen.count(id = status.getUniqueID())) { + } else if (recursive && sys::fs::is_directory(status) && !seen.count(id = status.getUniqueID())) { curr.push_back(path); seen.insert(id); } diff --git a/src/filesystem.hh b/src/filesystem.hh index 81222a55..6b7f9876 100644 --- a/src/filesystem.hh +++ b/src/filesystem.hh @@ -9,6 +9,5 @@ #include #include -void getFilesInFolder(std::string folder, bool recursive, - bool add_folder_to_path, +void getFilesInFolder(std::string folder, bool recursive, bool add_folder_to_path, const std::function &handler); diff --git a/src/fuzzy_match.cc b/src/fuzzy_match.cc index ddb2c3a6..df0ce8c6 100644 --- a/src/fuzzy_match.cc +++ b/src/fuzzy_match.cc @@ -32,9 +32,7 @@ void calculateRoles(std::string_view s, int roles[], int *class_set) { if (cur == Other) return None; // U(U)L is Head while U(U)U is Tail - return pre == Other || (cur == Upper && (pre == Lower || suc == Lower)) - ? Head - : Tail; + return pre == Other || (cur == Upper && (pre == Lower || suc == Lower)) ? Head : Tail; }; for (size_t i = 0; i < s.size() - 1; i++) { suc = getCharClass(s[i + 1]); @@ -117,16 +115,12 @@ int FuzzyMatcher::match(std::string_view text, bool strict) { int(*cur)[2] = dp[(i + 1) & 1]; cur[i][0] = cur[i][1] = kMinScore; for (int j = i; j < n; j++) { - cur[j + 1][0] = std::max(cur[j][0] + missScore(j, false), - cur[j][1] + missScore(j, true)); + cur[j + 1][0] = std::max(cur[j][0] + missScore(j, false), cur[j][1] + missScore(j, true)); // For the first char of pattern, apply extra restriction to filter bad // candidates (e.g. |int| in |PRINT|) cur[j + 1][1] = (case_sensitivity ? pat[i] == text[j] - : low_pat[i] == low_text[j] && - (i || text_role[j] != Tail || - pat[i] == text[j])) - ? std::max(pre[j][0] + matchScore(i, j, false), - pre[j][1] + matchScore(i, j, true)) + : low_pat[i] == low_text[j] && (i || text_role[j] != Tail || pat[i] == text[j])) + ? std::max(pre[j][0] + matchScore(i, j, false), pre[j][1] + matchScore(i, j, true)) : kMinScore * 2; } } diff --git a/src/hierarchy.hh b/src/hierarchy.hh index 24c2882c..7387aa92 100644 --- a/src/hierarchy.hh +++ b/src/hierarchy.hh @@ -9,8 +9,7 @@ #include namespace ccls { -template -std::vector flattenHierarchy(const std::optional &root) { +template std::vector flattenHierarchy(const std::optional &root) { if (!root) return {}; std::vector ret; diff --git a/src/indexer.cc b/src/indexer.cc index dff4ae2d..17d8cc2d 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -86,8 +86,7 @@ struct IndexParam { if (!vfs.stamp(path, it->second.mtime, no_linkage ? 3 : 1)) return; - it->second.db = - std::make_unique(path, it->second.content, no_linkage); + it->second.db = std::make_unique(path, it->second.content, no_linkage); } } @@ -110,19 +109,14 @@ struct IndexParam { } }; -StringRef getSourceInRange(const SourceManager &sm, const LangOptions &langOpts, - SourceRange sr) { +StringRef getSourceInRange(const SourceManager &sm, const LangOptions &langOpts, SourceRange sr) { SourceLocation bloc = sr.getBegin(), eLoc = sr.getEnd(); - std::pair bInfo = sm.getDecomposedLoc(bloc), - eInfo = sm.getDecomposedLoc(eLoc); + std::pair bInfo = sm.getDecomposedLoc(bloc), eInfo = sm.getDecomposedLoc(eLoc); bool invalid = false; StringRef buf = sm.getBufferData(bInfo.first, &invalid); if (invalid) return ""; - return buf.substr(bInfo.second, - eInfo.second + - Lexer::MeasureTokenLength(eLoc, sm, langOpts) - - bInfo.second); + return buf.substr(bInfo.second, eInfo.second + Lexer::MeasureTokenLength(eLoc, sm, langOpts) - bInfo.second); } Kind getKind(const Decl *d, SymbolKind &kind) { @@ -350,9 +344,7 @@ try_again: if (const RecordType *record = tp->getAs()) d = record->getDecl(); else - d = cast(tp) - ->getTemplateName() - .getAsTemplateDecl(); + d = cast(tp)->getTemplateName().getAsTemplateDecl(); break; case Type::Auto: @@ -383,9 +375,8 @@ const Decl *getAdjustedDecl(const Decl *d) { if (auto *r = dyn_cast(d)) { if (auto *s = dyn_cast(r)) { if (!s->isExplicitSpecialization()) { - llvm::PointerUnion - result = s->getSpecializedTemplateOrPartial(); + llvm::PointerUnion result = + s->getSpecializedTemplateOrPartial(); if (result.is()) d = result.get(); else @@ -465,8 +456,7 @@ public: } else { // Other lines, skip |pad| bytes int prefix = pad; - while (prefix > 0 && p < e && - (*p == ' ' || *p == '/' || *p == '*' || *p == '<' || *p == '!')) + while (prefix > 0 && p < e && (*p == ' ' || *p == '/' || *p == '*' || *p == '<' || *p == '!')) prefix--, p++; } ret.insert(ret.end(), p, q); @@ -527,8 +517,7 @@ public: } template - void setName(const Decl *d, std::string_view short_name, - std::string_view qualified, Def &def) { + void setName(const Decl *d, std::string_view short_name, std::string_view qualified, Def &def) { SmallString<256> str; llvm::raw_svector_ostream os(str); d->print(os, getDefaultPolicy()); @@ -544,8 +533,7 @@ public: auto i = name.find(short_name); if (short_name.size()) while (i != std::string::npos && - ((i && isAsciiIdentifierContinue(name[i - 1])) || - isAsciiIdentifierContinue(name[i + short_name.size()]))) + ((i && isAsciiIdentifierContinue(name[i - 1])) || isAsciiIdentifierContinue(name[i + short_name.size()]))) i = name.find(short_name, i + short_name.size()); if (i == std::string::npos) { // e.g. operator type-parameter-1 @@ -569,16 +557,14 @@ public: paren++; else if (name[i - 1] == '(') paren--; - else if (!(paren > 0 || isAsciiIdentifierContinue(name[i - 1]) || - name[i - 1] == ':')) + else if (!(paren > 0 || isAsciiIdentifierContinue(name[i - 1]) || name[i - 1] == ':')) break; } def.qual_name_offset = i; def.detailed_name = intern(name); } - void setVarName(const Decl *d, std::string_view short_name, - std::string_view qualified, IndexVar::Def &def) { + void setVarName(const Decl *d, std::string_view short_name, std::string_view qualified, IndexVar::Def &def) { QualType t; const Expr *init = nullptr; bool deduced = false; @@ -610,8 +596,7 @@ public: llvm::raw_svector_ostream os(str); PrintingPolicy pp = getDefaultPolicy(); t.print(os, pp); - if (str.size() && - (str.back() != ' ' && str.back() != '*' && str.back() != '&')) + if (str.size() && (str.back() != ' ' && str.back() != '*' && str.back() != '&')) str += ' '; def.qual_name_offset = str.size(); def.short_name_offset = str.size() + qualified.size() - short_name.size(); @@ -624,21 +609,17 @@ public: if (init) { SourceManager &sm = ctx->getSourceManager(); const LangOptions &lang = ctx->getLangOpts(); - SourceRange sr = - sm.getExpansionRange(init->getSourceRange()).getAsRange(); + SourceRange sr = sm.getExpansionRange(init->getSourceRange()).getAsRange(); SourceLocation l = d->getLocation(); if (l.isMacroID() || !sm.isBeforeInTranslationUnit(l, sr.getBegin())) return; StringRef buf = getSourceInRange(sm, lang, sr); Twine init = buf.count('\n') <= g_config->index.maxInitializerLines - 1 - ? buf.size() && buf[0] == ':' ? Twine(" ", buf) - : Twine(" = ", buf) + ? buf.size() && buf[0] == ':' ? Twine(" ", buf) : Twine(" = ", buf) : Twine(); Twine t = def.detailed_name + init; - def.hover = - def.storage == SC_Static && strncmp(def.detailed_name, "static ", 7) - ? intern(("static " + t).str()) - : intern(t.str()); + def.hover = def.storage == SC_Static && strncmp(def.detailed_name, "static ", 7) ? intern(("static " + t).str()) + : intern(t.str()); } } @@ -660,8 +641,7 @@ public: return it->second.first; } - void addMacroUse(IndexFile *db, SourceManager &sm, Usr usr, Kind kind, - SourceLocation sl) const { + void addMacroUse(IndexFile *db, SourceManager &sm, Usr usr, Kind kind, SourceLocation sl) const { FileID fid = sm.getFileID(sl); int lid = getFileLID(db, sm, fid); if (lid < 0) @@ -691,8 +671,7 @@ public: int offset; std::tie(rd, offset) = stack.back(); stack.pop_back(); - if (!rd->isCompleteDefinition() || rd->isDependentType() || - rd->isInvalidDecl() || !validateRecord(rd)) + if (!rd->isCompleteDefinition() || rd->isDependentType() || rd->isInvalidDecl() || !validateRecord(rd)) offset = -1; for (FieldDecl *fd : rd->fields()) { int offset1 = offset < 0 ? -1 : int(offset + ctx->getFieldOffset(fd)); @@ -710,10 +689,8 @@ public: public: IndexDataConsumer(IndexParam ¶m) : param(param) {} void initialize(ASTContext &ctx) override { this->ctx = param.ctx = &ctx; } - bool handleDeclOccurrence(const Decl *d, index::SymbolRoleSet roles, - ArrayRef relations, - SourceLocation src_loc, - ASTNodeInfo ast_node) override { + bool handleDeclOccurrence(const Decl *d, index::SymbolRoleSet roles, ArrayRef relations, + SourceLocation src_loc, ASTNodeInfo ast_node) override { if (!param.no_linkage) { if (auto *nd = dyn_cast(d); nd && nd->hasLinkage()) ; @@ -725,9 +702,7 @@ public: FileID fid; SourceLocation spell = sm.getSpellingLoc(src_loc); Range loc; - auto r = sm.isMacroArgExpansion(src_loc) - ? CharSourceRange::getTokenRange(spell) - : sm.getExpansionRange(src_loc); + auto r = sm.isMacroArgExpansion(src_loc) ? CharSourceRange::getTokenRange(spell) : sm.getExpansionRange(src_loc); loc = fromCharSourceRange(sm, lang, r); fid = sm.getFileID(r.getBegin()); if (fid.isInvalid()) @@ -753,11 +728,9 @@ public: const DeclContext *lex_dc = ast_node.ContainerDC->getRedeclContext(); { const NamespaceDecl *nd; - while ((nd = dyn_cast(cast(sem_dc))) && - nd->isAnonymousNamespace()) + while ((nd = dyn_cast(cast(sem_dc))) && nd->isAnonymousNamespace()) sem_dc = nd->getDeclContext()->getRedeclContext(); - while ((nd = dyn_cast(cast(lex_dc))) && - nd->isAnonymousNamespace()) + while ((nd = dyn_cast(cast(lex_dc))) && nd->isAnonymousNamespace()) lex_dc = nd->getDeclContext()->getRedeclContext(); } Role role = static_cast(roles); @@ -780,8 +753,7 @@ public: case Decl::CXXMethod: // *operator*= => *operator=* case Decl::Function: // operator delete if (src_loc.isFileID()) { - SourceRange sr = - cast(origD)->getNameInfo().getSourceRange(); + SourceRange sr = cast(origD)->getNameInfo().getSourceRange(); if (sr.getEnd().isFileID()) loc = fromTokenRange(sm, lang, sr); } @@ -803,14 +775,12 @@ public: Use use{{loc, role}, lid}; if (is_def) { SourceRange sr = origD->getSourceRange(); - entity->def.spell = {use, - fromTokenRangeDefaulted(sm, lang, sr, fid, loc)}; + entity->def.spell = {use, fromTokenRangeDefaulted(sm, lang, sr, fid, loc)}; entity->def.parent_kind = SymbolKind::File; getKind(cast(sem_dc), entity->def.parent_kind); } else if (is_decl) { SourceRange sr = origD->getSourceRange(); - entity->declarations.push_back( - {use, fromTokenRangeDefaulted(sm, lang, sr, fid, loc)}); + entity->declarations.push_back({use, fromTokenRangeDefaulted(sm, lang, sr, fid, loc)}); } else { entity->uses.push_back(use); return; @@ -821,8 +791,7 @@ public: switch (kind) { case Kind::Invalid: if (ls_kind == SymbolKind::Unknown) - LOG_S(INFO) << "Unhandled " << int(d->getKind()) << " " - << info->qualified << " in " << db->path << ":" + LOG_S(INFO) << "Unhandled " << int(d->getKind()) << " " << info->qualified << " in " << db->path << ":" << (loc.start.line + 1) << ":" << (loc.start.column + 1); return true; case Kind::File: @@ -831,9 +800,7 @@ public: func = &db->toFunc(usr); func->def.kind = ls_kind; // Mark as Role::Implicit to span one more column to the left/right. - if (!is_def && !is_decl && - (d->getKind() == Decl::CXXConstructor || - d->getKind() == Decl::CXXConversion)) + if (!is_def && !is_decl && (d->getKind() == Decl::CXXConstructor || d->getKind() == Decl::CXXConversion)) role = Role(role | Role::Implicit); do_def_decl(func); if (spell != src_loc) @@ -847,8 +814,7 @@ public: } else { const Decl *dc = cast(lex_dc); if (getKind(dc, ls_kind) == Kind::Func) - db->toFunc(getUsr(dc)) - .def.callees.push_back({loc, usr, Kind::Func, role}); + db->toFunc(getUsr(dc)).def.callees.push_back({loc, usr, Kind::Func, role}); } break; case Kind::Type: @@ -857,8 +823,7 @@ public: do_def_decl(type); if (spell != src_loc) addMacroUse(db, sm, usr, Kind::Type, spell); - if ((is_def || type->def.detailed_name[0] == '\0') && - info->short_name.size()) { + if ((is_def || type->def.detailed_name[0] == '\0') && info->short_name.size()) { if (d->getKind() == Decl::TemplateTypeParm) type->def.detailed_name = intern(info->short_name); else @@ -907,9 +872,8 @@ public: // e.g. lambda parameter SourceLocation l = d->getLocation(); if (sm.getFileID(l) == fid) { - var->def.spell = { - Use{{fromTokenRange(sm, lang, {l, l}), Role::Definition}, lid}, - fromTokenRange(sm, lang, d->getSourceRange())}; + var->def.spell = {Use{{fromTokenRange(sm, lang, {l, l}), Role::Definition}, lid}, + fromTokenRange(sm, lang, d->getSourceRange())}; var->def.parent_kind = SymbolKind::Method; } } @@ -942,8 +906,7 @@ public: auto *rd = dyn_cast(d); if (rd && rd->hasDefinition()) for (const CXXBaseSpecifier &base : rd->bases()) - if (const Decl *baseD = - getAdjustedDecl(getTypeDecl(base.getType()))) { + if (const Decl *baseD = getAdjustedDecl(getTypeDecl(base.getType()))) { Usr usr1 = getUsr(baseD); type->def.bases.push_back(usr1); db->toType(usr1).derived.push_back(usr); @@ -999,9 +962,8 @@ public: if (auto *sd = dyn_cast(rd)) d1 = sd->getSpecializedTemplate(); else if (auto *sd = dyn_cast(rd)) { - llvm::PointerUnion - result = sd->getSpecializedTemplateOrPartial(); + llvm::PointerUnion result = + sd->getSpecializedTemplateOrPartial(); if (result.is()) d1 = result.get(); else @@ -1032,8 +994,7 @@ public: const TypeSourceInfo *tsi = td->getTypeSourceInfo(); SourceLocation l1 = tsi->getTypeLoc().getBeginLoc(); if (sm.getFileID(l1) == fid) - type1.uses.push_back( - {{fromTokenRange(sm, lang, {l1, l1}), Role::Reference}, lid}); + type1.uses.push_back({{fromTokenRange(sm, lang, {l1, l1}), Role::Reference}, lid}); } } } @@ -1056,8 +1017,7 @@ public: auto *ecd = cast(d); const auto &val = ecd->getInitVal(); std::string init = - " = " + (val.isSigned() ? std::to_string(val.getSExtValue()) - : std::to_string(val.getZExtValue())); + " = " + (val.isSigned() ? std::to_string(val.getSExtValue()) : std::to_string(val.getZExtValue())); var->def.hover = intern(var->def.detailed_name + init); } break; @@ -1080,15 +1040,12 @@ class IndexPPCallbacks : public PPCallbacks { } public: - IndexPPCallbacks(SourceManager &sm, IndexParam ¶m) - : sm(sm), param(param) {} - void FileChanged(SourceLocation sl, FileChangeReason reason, - SrcMgr::CharacteristicKind, FileID) override { + IndexPPCallbacks(SourceManager &sm, IndexParam ¶m) : sm(sm), param(param) {} + void FileChanged(SourceLocation sl, FileChangeReason reason, SrcMgr::CharacteristicKind, FileID) override { if (reason == FileChangeReason::EnterFile) (void)param.consumeFile(sm.getFileID(sl)); } - void InclusionDirective(SourceLocation hashLoc, const Token &tok, - StringRef included, bool isAngled, + void InclusionDirective(SourceLocation hashLoc, const Token &tok, StringRef included, bool isAngled, CharSourceRange filenameRange, #if LLVM_VERSION_MAJOR >= 16 // llvmorg-16-init-15080-g854c10f8d185 OptionalFileEntryRef fileRef, @@ -1097,8 +1054,7 @@ public: #else const FileEntry *file, #endif - StringRef searchPath, StringRef relativePath, - const clang::Module *suggestedModule, + StringRef searchPath, StringRef relativePath, const clang::Module *suggestedModule, #if LLVM_VERSION_MAJOR >= 19 // llvmorg-19-init-1720-gda95d926f6fc bool moduleImported, #endif @@ -1108,8 +1064,7 @@ public: #endif if (!file) return; - auto spell = fromCharSourceRange(sm, param.ctx->getLangOpts(), - filenameRange, nullptr); + auto spell = fromCharSourceRange(sm, param.ctx->getLangOpts(), filenameRange, nullptr); FileID fid = sm.getFileID(filenameRange.getBegin()); if (IndexFile *db = param.consumeFile(fid)) { #if LLVM_VERSION_MAJOR < 19 @@ -1141,34 +1096,28 @@ public: var.def.detailed_name = intern(name); var.def.short_name_size = name.size(); StringRef buf = getSourceInRange(sm, lang, sr); - var.def.hover = - intern(buf.count('\n') <= g_config->index.maxInitializerLines - 1 - ? Twine("#define ", getSourceInRange(sm, lang, sr)).str() - : Twine("#define ", name).str()); + var.def.hover = intern(buf.count('\n') <= g_config->index.maxInitializerLines - 1 + ? Twine("#define ", getSourceInRange(sm, lang, sr)).str() + : Twine("#define ", name).str()); } } } - void MacroExpands(const Token &tok, const MacroDefinition &, SourceRange sr, - const MacroArgs *) override { + void MacroExpands(const Token &tok, const MacroDefinition &, SourceRange sr, const MacroArgs *) override { SourceLocation sl = sm.getSpellingLoc(sr.getBegin()); FileID fid = sm.getFileID(sl); if (IndexFile *db = param.consumeFile(fid)) { IndexVar &var = db->toVar(getMacro(tok).second); - var.uses.push_back( - {{fromTokenRange(sm, param.ctx->getLangOpts(), {sl, sl}, nullptr), - Role::Dynamic}}); + var.uses.push_back({{fromTokenRange(sm, param.ctx->getLangOpts(), {sl, sl}, nullptr), Role::Dynamic}}); } } - void MacroUndefined(const Token &tok, const MacroDefinition &md, - const MacroDirective *ud) override { + void MacroUndefined(const Token &tok, const MacroDefinition &md, const MacroDirective *ud) override { if (ud) { SourceLocation sl = ud->getLocation(); MacroExpands(tok, md, {sl, sl}, nullptr); } } void SourceRangeSkipped(SourceRange sr, SourceLocation) override { - Range range = fromCharSourceRange(sm, param.ctx->getLangOpts(), - CharSourceRange::getCharRange(sr)); + Range range = fromCharSourceRange(sm, param.ctx->getLangOpts(), CharSourceRange::getCharRange(sr)); FileID fid = sm.getFileID(sr.getBegin()); if (fid.isValid()) if (IndexFile *db = param.consumeFile(fid)) @@ -1182,13 +1131,10 @@ class IndexFrontendAction : public ASTFrontendAction { IndexParam ¶m; public: - IndexFrontendAction(std::shared_ptr dataConsumer, - const index::IndexingOptions &indexOpts, + IndexFrontendAction(std::shared_ptr dataConsumer, const index::IndexingOptions &indexOpts, IndexParam ¶m) - : dataConsumer(std::move(dataConsumer)), indexOpts(indexOpts), - param(param) {} - std::unique_ptr CreateASTConsumer(CompilerInstance &ci, - StringRef inFile) override { + : dataConsumer(std::move(dataConsumer)), indexOpts(indexOpts), param(param) {} + std::unique_ptr CreateASTConsumer(CompilerInstance &ci, StringRef inFile) override { class SkipProcessed : public ASTConsumer { IndexParam ¶m; const ASTContext *ctx = nullptr; @@ -1199,18 +1145,15 @@ public: bool shouldSkipFunctionBody(Decl *d) override { const SourceManager &sm = ctx->getSourceManager(); FileID fid = sm.getFileID(sm.getExpansionLoc(d->getLocation())); - return !(g_config->index.multiVersion && param.useMultiVersion(fid)) && - !param.consumeFile(fid); + return !(g_config->index.multiVersion && param.useMultiVersion(fid)) && !param.consumeFile(fid); } }; std::shared_ptr pp = ci.getPreprocessorPtr(); - pp->addPPCallbacks( - std::make_unique(pp->getSourceManager(), param)); + pp->addPPCallbacks(std::make_unique(pp->getSourceManager(), param)); std::vector> consumers; consumers.push_back(std::make_unique(param)); - consumers.push_back(index::createIndexingASTConsumer( - dataConsumer, indexOpts, std::move(pp))); + consumers.push_back(index::createIndexingASTConsumer(dataConsumer, indexOpts, std::move(pp))); return std::make_unique(std::move(consumers)); } }; @@ -1218,8 +1161,7 @@ public: class IndexDiags : public DiagnosticConsumer { public: llvm::SmallString<64> message; - void HandleDiagnostic(DiagnosticsEngine::Level level, - const clang::Diagnostic &info) override { + void HandleDiagnostic(DiagnosticsEngine::Level level, const clang::Diagnostic &info) override { DiagnosticConsumer::HandleDiagnostic(level, info); if (message.empty()) info.FormatDiagnostic(message); @@ -1230,8 +1172,7 @@ public: const int IndexFile::kMajorVersion = 21; const int IndexFile::kMinorVersion = 0; -IndexFile::IndexFile(const std::string &path, const std::string &contents, - bool no_linkage) +IndexFile::IndexFile(const std::string &path, const std::string &contents, bool no_linkage) : path(path), no_linkage(no_linkage), file_contents(contents) {} IndexFunc &IndexFile::toFunc(Usr usr) { @@ -1255,9 +1196,7 @@ IndexVar &IndexFile::toVar(Usr usr) { return it->second; } -std::string IndexFile::toString() { - return ccls::serialize(SerializeFormat::Json, *this); -} +std::string IndexFile::toString() { return ccls::serialize(SerializeFormat::Json, *this); } template void uniquify(std::vector &a) { std::unordered_set seen; @@ -1270,22 +1209,16 @@ template void uniquify(std::vector &a) { namespace idx { void init() { - multiVersionMatcher = new GroupMatch(g_config->index.multiVersionWhitelist, - g_config->index.multiVersionBlacklist); + multiVersionMatcher = new GroupMatch(g_config->index.multiVersionWhitelist, g_config->index.multiVersionBlacklist); } -IndexResult -index(SemaManager *manager, WorkingFiles *wfiles, VFS *vfs, - const std::string &opt_wdir, const std::string &main, - const std::vector &args, - const std::vector> &remapped, - bool no_linkage, bool &ok) { +IndexResult index(SemaManager *manager, WorkingFiles *wfiles, VFS *vfs, const std::string &opt_wdir, + const std::string &main, const std::vector &args, + const std::vector> &remapped, bool no_linkage, bool &ok) { ok = true; auto pch = std::make_shared(); - llvm::IntrusiveRefCntPtr fs = - llvm::vfs::getRealFileSystem(); - std::shared_ptr ci = - buildCompilerInvocation(main, args, fs); + llvm::IntrusiveRefCntPtr fs = llvm::vfs::getRealFileSystem(); + std::shared_ptr ci = buildCompilerInvocation(main, args, fs); // e.g. .s if (!ci) return {}; @@ -1293,12 +1226,10 @@ index(SemaManager *manager, WorkingFiles *wfiles, VFS *vfs, // -fparse-all-comments enables documentation in the indexer and in // code completion. #if LLVM_VERSION_MAJOR >= 18 - ci->getLangOpts().CommentOpts.ParseAllComments = - g_config->index.comments > 1; + ci->getLangOpts().CommentOpts.ParseAllComments = g_config->index.comments > 1; ci->getLangOpts().RetainCommentsFromSystemHeaders = true; #else - ci->getLangOpts()->CommentOpts.ParseAllComments = - g_config->index.comments > 1; + ci->getLangOpts()->CommentOpts.ParseAllComments = g_config->index.comments > 1; ci->getLangOpts()->RetainCommentsFromSystemHeaders = true; #endif std::string buf = wfiles->getContent(main); @@ -1318,30 +1249,25 @@ index(SemaManager *manager, WorkingFiles *wfiles, VFS *vfs, #endif &dc, false); clang->getDiagnostics().setIgnoreAllWarnings(true); - clang->setTarget(TargetInfo::CreateTargetInfo( - clang->getDiagnostics(), clang->getInvocation().TargetOpts)); + clang->setTarget(TargetInfo::CreateTargetInfo(clang->getDiagnostics(), clang->getInvocation().TargetOpts)); if (!clang->hasTarget()) return {}; clang->getPreprocessorOpts().RetainRemappedFileBuffers = true; clang->createFileManager(fs); - clang->setSourceManager(new SourceManager(clang->getDiagnostics(), - clang->getFileManager(), true)); + clang->setSourceManager(new SourceManager(clang->getDiagnostics(), clang->getFileManager(), true)); IndexParam param(*vfs, no_linkage); index::IndexingOptions indexOpts; - indexOpts.SystemSymbolFilter = - index::IndexingOptions::SystemSymbolFilterKind::All; + indexOpts.SystemSymbolFilter = index::IndexingOptions::SystemSymbolFilterKind::All; if (no_linkage) { indexOpts.IndexFunctionLocals = true; indexOpts.IndexImplicitInstantiation = true; - indexOpts.IndexParametersInDeclarations = - g_config->index.parametersInDeclarations; + indexOpts.IndexParametersInDeclarations = g_config->index.parametersInDeclarations; indexOpts.IndexTemplateParameters = true; } - auto action = std::make_unique( - std::make_shared(param), indexOpts, param); + auto action = std::make_unique(std::make_shared(param), indexOpts, param); std::string reason; { llvm::CrashRecoveryContext crc; @@ -1361,8 +1287,7 @@ index(SemaManager *manager, WorkingFiles *wfiles, VFS *vfs, } } if (!ok) { - LOG_S(ERROR) << "failed to index " << main - << (reason.empty() ? "" : ": " + reason); + LOG_S(ERROR) << "failed to index " << main << (reason.empty() ? "" : ": " + reason); return {}; } @@ -1403,8 +1328,7 @@ index(SemaManager *manager, WorkingFiles *wfiles, VFS *vfs, if (path == entry->path) entry->mtime = file.mtime; else if (path != entry->import_file) - entry->dependencies[llvm::CachedHashStringRef(intern(path))] = - file.mtime; + entry->dependencies[llvm::CachedHashStringRef(intern(path))] = file.mtime; } result.indexes.push_back(std::move(entry)); } @@ -1443,22 +1367,20 @@ void reflect(JsonReader &vis, DeclRef &v) { void reflect(JsonWriter &vis, SymbolRef &v) { char buf[99]; - snprintf(buf, sizeof buf, "%s|%" PRIu64 "|%d|%d", v.range.toString().c_str(), - v.usr, int(v.kind), int(v.role)); + snprintf(buf, sizeof buf, "%s|%" PRIu64 "|%d|%d", v.range.toString().c_str(), v.usr, int(v.kind), int(v.role)); std::string s(buf); reflect(vis, s); } void reflect(JsonWriter &vis, Use &v) { char buf[99]; - snprintf(buf, sizeof buf, "%s|%d|%d", v.range.toString().c_str(), int(v.role), - v.file_id); + snprintf(buf, sizeof buf, "%s|%d|%d", v.range.toString().c_str(), int(v.role), v.file_id); std::string s(buf); reflect(vis, s); } void reflect(JsonWriter &vis, DeclRef &v) { char buf[99]; - snprintf(buf, sizeof buf, "%s|%s|%d|%d", v.range.toString().c_str(), - v.extent.toString().c_str(), int(v.role), v.file_id); + snprintf(buf, sizeof buf, "%s|%s|%d|%d", v.range.toString().c_str(), v.extent.toString().c_str(), int(v.role), + v.file_id); std::string s(buf); reflect(vis, s); } diff --git a/src/indexer.hh b/src/indexer.hh index 8aea31e6..d72a440e 100644 --- a/src/indexer.hh +++ b/src/indexer.hh @@ -51,23 +51,15 @@ enum class Role : uint16_t { All = (1 << 9) - 1, }; REFLECT_UNDERLYING_B(Role); -inline uint16_t operator&(Role lhs, Role rhs) { - return uint16_t(lhs) & uint16_t(rhs); -} -inline Role operator|(Role lhs, Role rhs) { - return Role(uint16_t(lhs) | uint16_t(rhs)); -} +inline uint16_t operator&(Role lhs, Role rhs) { return uint16_t(lhs) & uint16_t(rhs); } +inline Role operator|(Role lhs, Role rhs) { return Role(uint16_t(lhs) | uint16_t(rhs)); } struct SymbolIdx { Usr usr; Kind kind; - bool operator==(const SymbolIdx &o) const { - return usr == o.usr && kind == o.kind; - } - bool operator<(const SymbolIdx &o) const { - return usr != o.usr ? usr < o.usr : kind < o.kind; - } + bool operator==(const SymbolIdx &o) const { return usr == o.usr && kind == o.kind; } + bool operator<(const SymbolIdx &o) const { return usr != o.usr ? usr < o.usr : kind < o.kind; } }; // |id,kind| refer to the referenced entity. @@ -77,18 +69,14 @@ struct SymbolRef { Kind kind; Role role; operator SymbolIdx() const { return {usr, kind}; } - std::tuple toTuple() const { - return std::make_tuple(range, usr, kind, role); - } + std::tuple toTuple() const { return std::make_tuple(range, usr, kind, role); } bool operator==(const SymbolRef &o) const { return toTuple() == o.toTuple(); } bool valid() const { return range.valid(); } }; struct ExtentRef : SymbolRef { Range extent; - std::tuple toTuple() const { - return std::make_tuple(range, usr, kind, role, extent); - } + std::tuple toTuple() const { return std::make_tuple(range, usr, kind, role, extent); } bool operator==(const ExtentRef &o) const { return toTuple() == o.toTuple(); } }; @@ -97,9 +85,7 @@ struct Ref { Role role; bool valid() const { return range.valid(); } - std::tuple toTuple() const { - return std::make_tuple(range, role); - } + std::tuple toTuple() const { return std::make_tuple(range, role); } bool operator==(const Ref &o) const { return toTuple() == o.toTuple(); } bool operator<(const Ref &o) const { return toTuple() < o.toTuple(); } }; @@ -143,18 +129,13 @@ template using VectorAdapter = std::vector>; template struct NameMixin { std::string_view name(bool qualified) const { auto self = static_cast(this); - return qualified - ? std::string_view(self->detailed_name + self->qual_name_offset, - self->short_name_offset - - self->qual_name_offset + - self->short_name_size) - : std::string_view(self->detailed_name + self->short_name_offset, - self->short_name_size); + return qualified ? std::string_view(self->detailed_name + self->qual_name_offset, + self->short_name_offset - self->qual_name_offset + self->short_name_size) + : std::string_view(self->detailed_name + self->short_name_offset, self->short_name_size); } }; -template