Path: blob/main/contrib/llvm-project/clang/lib/Index/FileIndexRecord.cpp
35233 views
//===--- FileIndexRecord.cpp - Index data per file --------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "FileIndexRecord.h"9#include "clang/AST/ASTContext.h"10#include "clang/AST/DeclTemplate.h"11#include "clang/Basic/SourceManager.h"12#include "llvm/ADT/SmallString.h"13#include "llvm/Support/Path.h"1415using namespace clang;16using namespace clang::index;1718ArrayRef<DeclOccurrence>19FileIndexRecord::getDeclOccurrencesSortedByOffset() const {20if (!IsSorted) {21llvm::stable_sort(Decls,22[](const DeclOccurrence &A, const DeclOccurrence &B) {23return A.Offset < B.Offset;24});25IsSorted = true;26}27return Decls;28}2930void FileIndexRecord::addDeclOccurence(SymbolRoleSet Roles, unsigned Offset,31const Decl *D,32ArrayRef<SymbolRelation> Relations) {33assert(D->isCanonicalDecl() &&34"Occurrences should be associated with their canonical decl");35IsSorted = false;36Decls.emplace_back(Roles, Offset, D, Relations);37}3839void FileIndexRecord::addMacroOccurence(SymbolRoleSet Roles, unsigned Offset,40const IdentifierInfo *Name,41const MacroInfo *MI) {42IsSorted = false;43Decls.emplace_back(Roles, Offset, Name, MI);44}4546void FileIndexRecord::removeHeaderGuardMacros() {47llvm::erase_if(Decls, [](const DeclOccurrence &D) {48if (const auto *MI = D.DeclOrMacro.dyn_cast<const MacroInfo *>())49return MI->isUsedForHeaderGuard();50return false;51});52}5354void FileIndexRecord::print(llvm::raw_ostream &OS, SourceManager &SM) const {55OS << "DECLS BEGIN ---\n";56for (auto &DclInfo : Decls) {57if (const auto *D = DclInfo.DeclOrMacro.dyn_cast<const Decl *>()) {58SourceLocation Loc = SM.getFileLoc(D->getLocation());59PresumedLoc PLoc = SM.getPresumedLoc(Loc);60OS << llvm::sys::path::filename(PLoc.getFilename()) << ':'61<< PLoc.getLine() << ':' << PLoc.getColumn();6263if (const auto *ND = dyn_cast<NamedDecl>(D)) {64OS << ' ' << ND->getDeclName();65}66} else {67const auto *MI = DclInfo.DeclOrMacro.get<const MacroInfo *>();68SourceLocation Loc = SM.getFileLoc(MI->getDefinitionLoc());69PresumedLoc PLoc = SM.getPresumedLoc(Loc);70OS << llvm::sys::path::filename(PLoc.getFilename()) << ':'71<< PLoc.getLine() << ':' << PLoc.getColumn();72OS << ' ' << DclInfo.MacroName->getName();73}7475OS << '\n';76}77OS << "DECLS END ---\n";78}798081