2019-02-27 21:48:02 +00:00
|
|
|
//===--- FileIndexRecord.cpp - Index data per file --------------*- C++ -*-===//
|
2019-02-27 21:47:40 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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() {
|
|
|
|
auto It =
|
|
|
|
std::remove_if(Decls.begin(), Decls.end(), [](const DeclOccurrence &D) {
|
|
|
|
if (const auto *MI = D.DeclOrMacro.dyn_cast<const MacroInfo *>())
|
|
|
|
return MI->isUsedForHeaderGuard();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
Decls.erase(It, Decls.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
[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
|
|
|
if (const auto *D = DclInfo.DeclOrMacro.dyn_cast<const Decl *>()) {
|
|
|
|
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 {
|
|
|
|
const auto *MI = DclInfo.DeclOrMacro.get<const MacroInfo *>();
|
|
|
|
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";
|
|
|
|
}
|