2019-02-27 21:48:02 +00:00
|
|
|
//===--- FileIndexRecord.cpp - Index data per file --------------*- C++ -*-===//
|
2019-02-27 21:47:40 +00:00
|
|
|
//
|
2021-08-05 03:16:17 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-02-27 21:47:40 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "FileIndexRecord.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2020-02-27 11:01:58 -08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2019-02-27 21:47:40 +00:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace clang::index;
|
|
|
|
|
2021-06-15 17:44:06 -07:00
|
|
|
ArrayRef<DeclOccurrence>
|
|
|
|
FileIndexRecord::getDeclOccurrencesSortedByOffset() const {
|
|
|
|
if (!IsSorted) {
|
|
|
|
llvm::stable_sort(Decls,
|
|
|
|
[](const DeclOccurrence &A, const DeclOccurrence &B) {
|
|
|
|
return A.Offset < B.Offset;
|
|
|
|
});
|
|
|
|
IsSorted = true;
|
2019-02-27 21:47:40 +00:00
|
|
|
}
|
2021-06-15 17:44:06 -07:00
|
|
|
return Decls;
|
[index] Improve macro indexing support
The major change here is to index macro occurrences in more places than
before, specifically
* In non-expansion references such as `#if`, `#ifdef`, etc.
* When the macro is a reference to a builtin macro such as __LINE__.
* When using the preprocessor state instead of callbacks, we now include
all definition locations and undefinitions instead of just the latest
one (which may also have had the wrong location previously).
* When indexing an existing module file (.pcm), we now include module
macros, and we no longer report unrelated preprocessor macros during
indexing the module, which could have caused duplication.
Additionally, we now correctly obey the system symbol filter for macros,
so by default in system headers only definition/undefinition occurrences
are reported, but it can be configured to report references as well if
desired.
Extends FileIndexRecord to support occurrences of macros. Since the
design of this type is to keep a single list of entities organized by
source location, we incorporate macros into the existing DeclOccurrence
struct.
Differential Revision: https://reviews.llvm.org/D99758
2021-03-23 15:22:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileIndexRecord::addDeclOccurence(SymbolRoleSet Roles, unsigned Offset,
|
|
|
|
const Decl *D,
|
|
|
|
ArrayRef<SymbolRelation> Relations) {
|
|
|
|
assert(D->isCanonicalDecl() &&
|
|
|
|
"Occurrences should be associated with their canonical decl");
|
2021-06-15 17:44:06 -07:00
|
|
|
IsSorted = false;
|
|
|
|
Decls.emplace_back(Roles, Offset, D, Relations);
|
2019-02-27 21:47:40 +00:00
|
|
|
}
|
|
|
|
|
[index] Improve macro indexing support
The major change here is to index macro occurrences in more places than
before, specifically
* In non-expansion references such as `#if`, `#ifdef`, etc.
* When the macro is a reference to a builtin macro such as __LINE__.
* When using the preprocessor state instead of callbacks, we now include
all definition locations and undefinitions instead of just the latest
one (which may also have had the wrong location previously).
* When indexing an existing module file (.pcm), we now include module
macros, and we no longer report unrelated preprocessor macros during
indexing the module, which could have caused duplication.
Additionally, we now correctly obey the system symbol filter for macros,
so by default in system headers only definition/undefinition occurrences
are reported, but it can be configured to report references as well if
desired.
Extends FileIndexRecord to support occurrences of macros. Since the
design of this type is to keep a single list of entities organized by
source location, we incorporate macros into the existing DeclOccurrence
struct.
Differential Revision: https://reviews.llvm.org/D99758
2021-03-23 15:22:58 -07:00
|
|
|
void FileIndexRecord::addMacroOccurence(SymbolRoleSet Roles, unsigned Offset,
|
|
|
|
const IdentifierInfo *Name,
|
|
|
|
const MacroInfo *MI) {
|
2021-06-15 17:44:06 -07:00
|
|
|
IsSorted = false;
|
|
|
|
Decls.emplace_back(Roles, Offset, Name, MI);
|
[index] Improve macro indexing support
The major change here is to index macro occurrences in more places than
before, specifically
* In non-expansion references such as `#if`, `#ifdef`, etc.
* When the macro is a reference to a builtin macro such as __LINE__.
* When using the preprocessor state instead of callbacks, we now include
all definition locations and undefinitions instead of just the latest
one (which may also have had the wrong location previously).
* When indexing an existing module file (.pcm), we now include module
macros, and we no longer report unrelated preprocessor macros during
indexing the module, which could have caused duplication.
Additionally, we now correctly obey the system symbol filter for macros,
so by default in system headers only definition/undefinition occurrences
are reported, but it can be configured to report references as well if
desired.
Extends FileIndexRecord to support occurrences of macros. Since the
design of this type is to keep a single list of entities organized by
source location, we incorporate macros into the existing DeclOccurrence
struct.
Differential Revision: https://reviews.llvm.org/D99758
2021-03-23 15:22:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileIndexRecord::removeHeaderGuardMacros() {
|
2022-08-13 12:55:48 -07:00
|
|
|
llvm::erase_if(Decls, [](const DeclOccurrence &D) {
|
|
|
|
if (const auto *MI = D.DeclOrMacro.dyn_cast<const MacroInfo *>())
|
|
|
|
return MI->isUsedForHeaderGuard();
|
|
|
|
return false;
|
|
|
|
});
|
[index] Improve macro indexing support
The major change here is to index macro occurrences in more places than
before, specifically
* In non-expansion references such as `#if`, `#ifdef`, etc.
* When the macro is a reference to a builtin macro such as __LINE__.
* When using the preprocessor state instead of callbacks, we now include
all definition locations and undefinitions instead of just the latest
one (which may also have had the wrong location previously).
* When indexing an existing module file (.pcm), we now include module
macros, and we no longer report unrelated preprocessor macros during
indexing the module, which could have caused duplication.
Additionally, we now correctly obey the system symbol filter for macros,
so by default in system headers only definition/undefinition occurrences
are reported, but it can be configured to report references as well if
desired.
Extends FileIndexRecord to support occurrences of macros. Since the
design of this type is to keep a single list of entities organized by
source location, we incorporate macros into the existing DeclOccurrence
struct.
Differential Revision: https://reviews.llvm.org/D99758
2021-03-23 15:22:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileIndexRecord::print(llvm::raw_ostream &OS, SourceManager &SM) const {
|
2019-02-27 21:47:40 +00:00
|
|
|
OS << "DECLS BEGIN ---\n";
|
|
|
|
for (auto &DclInfo : Decls) {
|
2025-01-25 01:16:00 -08:00
|
|
|
if (const auto *D = dyn_cast<const Decl *>(DclInfo.DeclOrMacro)) {
|
[index] Improve macro indexing support
The major change here is to index macro occurrences in more places than
before, specifically
* In non-expansion references such as `#if`, `#ifdef`, etc.
* When the macro is a reference to a builtin macro such as __LINE__.
* When using the preprocessor state instead of callbacks, we now include
all definition locations and undefinitions instead of just the latest
one (which may also have had the wrong location previously).
* When indexing an existing module file (.pcm), we now include module
macros, and we no longer report unrelated preprocessor macros during
indexing the module, which could have caused duplication.
Additionally, we now correctly obey the system symbol filter for macros,
so by default in system headers only definition/undefinition occurrences
are reported, but it can be configured to report references as well if
desired.
Extends FileIndexRecord to support occurrences of macros. Since the
design of this type is to keep a single list of entities organized by
source location, we incorporate macros into the existing DeclOccurrence
struct.
Differential Revision: https://reviews.llvm.org/D99758
2021-03-23 15:22:58 -07:00
|
|
|
SourceLocation Loc = SM.getFileLoc(D->getLocation());
|
|
|
|
PresumedLoc PLoc = SM.getPresumedLoc(Loc);
|
|
|
|
OS << llvm::sys::path::filename(PLoc.getFilename()) << ':'
|
|
|
|
<< PLoc.getLine() << ':' << PLoc.getColumn();
|
|
|
|
|
|
|
|
if (const auto *ND = dyn_cast<NamedDecl>(D)) {
|
|
|
|
OS << ' ' << ND->getDeclName();
|
|
|
|
}
|
|
|
|
} else {
|
2024-12-11 21:13:13 -08:00
|
|
|
const auto *MI = cast<const MacroInfo *>(DclInfo.DeclOrMacro);
|
[index] Improve macro indexing support
The major change here is to index macro occurrences in more places than
before, specifically
* In non-expansion references such as `#if`, `#ifdef`, etc.
* When the macro is a reference to a builtin macro such as __LINE__.
* When using the preprocessor state instead of callbacks, we now include
all definition locations and undefinitions instead of just the latest
one (which may also have had the wrong location previously).
* When indexing an existing module file (.pcm), we now include module
macros, and we no longer report unrelated preprocessor macros during
indexing the module, which could have caused duplication.
Additionally, we now correctly obey the system symbol filter for macros,
so by default in system headers only definition/undefinition occurrences
are reported, but it can be configured to report references as well if
desired.
Extends FileIndexRecord to support occurrences of macros. Since the
design of this type is to keep a single list of entities organized by
source location, we incorporate macros into the existing DeclOccurrence
struct.
Differential Revision: https://reviews.llvm.org/D99758
2021-03-23 15:22:58 -07:00
|
|
|
SourceLocation Loc = SM.getFileLoc(MI->getDefinitionLoc());
|
|
|
|
PresumedLoc PLoc = SM.getPresumedLoc(Loc);
|
|
|
|
OS << llvm::sys::path::filename(PLoc.getFilename()) << ':'
|
|
|
|
<< PLoc.getLine() << ':' << PLoc.getColumn();
|
|
|
|
OS << ' ' << DclInfo.MacroName->getName();
|
2019-02-27 21:47:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
OS << "DECLS END ---\n";
|
|
|
|
}
|