Skip to content

Commit

Permalink
Merged master:41eeded8043 into amd-gfx:936fb7dd0a6
Browse files Browse the repository at this point in the history
Local branch amd-gfx 936fb7d Merged master:82f6ae5433c into amd-gfx:f07e6001b81
Remote branch master 41eeded Fix the clang interpreter example which was broken by 4fc68b9.
  • Loading branch information
Sw authored and Sw committed Dec 5, 2019
2 parents 936fb7d + 41eeded commit 0f31e70
Show file tree
Hide file tree
Showing 232 changed files with 7,719 additions and 1,770 deletions.
4 changes: 2 additions & 2 deletions clang-tools-extra/clangd/ClangdServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ void ClangdServer::prepareRename(PathRef File, Position Pos,
const auto &SM = AST.getSourceManager();
SourceLocation Loc =
SM.getMacroArgExpandedLocation(getBeginningOfIdentifier(
Pos, AST.getSourceManager(), AST.getASTContext().getLangOpts()));
auto Range = getTokenRange(SM, AST.getASTContext().getLangOpts(), Loc);
Pos, AST.getSourceManager(), AST.getLangOpts()));
auto Range = getTokenRange(SM, AST.getLangOpts(), Loc);
if (!Range)
return CB(llvm::None); // "rename" is not valid at the position.

Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/clangd/HeaderSourceSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ llvm::Optional<Path> getCorrespondingHeaderOrSource(const Path &OriginalFile,
//
// For each symbol in the original file, we get its target location (decl or
// def) from the index, then award that target file.
bool IsHeader = isHeaderFile(OriginalFile, AST.getASTContext().getLangOpts());
bool IsHeader = isHeaderFile(OriginalFile, AST.getLangOpts());
Index->lookup(Request, [&](const Symbol &Sym) {
if (IsHeader)
AwardTarget(Sym.Definition.FileURI);
Expand Down
10 changes: 4 additions & 6 deletions clang-tools-extra/clangd/Hover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,7 @@ HoverInfo getHoverContents(const DefinedMacro &Macro, ParsedAST &AST) {
SourceLocation StartLoc = Macro.Info->getDefinitionLoc();
SourceLocation EndLoc = Macro.Info->getDefinitionEndLoc();
if (EndLoc.isValid()) {
EndLoc = Lexer::getLocForEndOfToken(EndLoc, 0, SM,
AST.getASTContext().getLangOpts());
EndLoc = Lexer::getLocForEndOfToken(EndLoc, 0, SM, AST.getLangOpts());
bool Invalid;
StringRef Buffer = SM.getBufferData(SM.getFileID(StartLoc), &Invalid);
if (!Invalid) {
Expand All @@ -391,7 +390,7 @@ llvm::Optional<HoverInfo> getHover(ParsedAST &AST, Position Pos,
const SourceManager &SM = AST.getSourceManager();
llvm::Optional<HoverInfo> HI;
SourceLocation SourceLocationBeg = SM.getMacroArgExpandedLocation(
getBeginningOfIdentifier(Pos, SM, AST.getASTContext().getLangOpts()));
getBeginningOfIdentifier(Pos, SM, AST.getLangOpts()));

if (auto Deduced = getDeducedType(AST.getASTContext(), SourceLocationBeg)) {
// Find the corresponding decl to populate kind and fetch documentation.
Expand Down Expand Up @@ -435,9 +434,8 @@ llvm::Optional<HoverInfo> getHover(ParsedAST &AST, Position Pos,
tooling::applyAllReplacements(HI->Definition, Replacements))
HI->Definition = *Formatted;

HI->SymRange =
getTokenRange(AST.getASTContext().getSourceManager(),
AST.getASTContext().getLangOpts(), SourceLocationBeg);
HI->SymRange = getTokenRange(AST.getASTContext().getSourceManager(),
AST.getLangOpts(), SourceLocationBeg);
return HI;
}

Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/clangd/ParsedAST.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ class ParsedAST {
return getASTContext().getSourceManager();
}

const LangOptions &getLangOpts() const {
return getASTContext().getLangOpts();
}

/// This function returns top-level decls present in the main file of the AST.
/// The result does not include the decls that come from the preamble.
/// (These should be const, but RecursiveASTVisitor requires Decl*).
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/clangd/SemanticSelection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ llvm::Expected<std::vector<Range>> getSemanticRanges(ParsedAST &AST,
Position Pos) {
std::vector<Range> Result;
const auto &SM = AST.getSourceManager();
const auto &LangOpts = AST.getASTContext().getLangOpts();
const auto &LangOpts = AST.getLangOpts();

auto FID = SM.getMainFileID();
auto Offset = positionToOffset(SM.getBufferData(FID), Pos);
Expand Down
21 changes: 9 additions & 12 deletions clang-tools-extra/clangd/XRefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,8 @@ std::vector<LocatedSymbol> locateSymbolAt(ParsedAST &AST, Position Pos,

// Macros are simple: there's no declaration/definition distinction.
// As a consequence, there's no need to look them up in the index either.
SourceLocation MaybeMacroLocation =
SM.getMacroArgExpandedLocation(getBeginningOfIdentifier(
Pos, AST.getSourceManager(), AST.getASTContext().getLangOpts()));
SourceLocation MaybeMacroLocation = SM.getMacroArgExpandedLocation(
getBeginningOfIdentifier(Pos, AST.getSourceManager(), AST.getLangOpts()));
std::vector<LocatedSymbol> Result;
if (auto M = locateMacroAt(MaybeMacroLocation, AST.getPreprocessor())) {
if (auto Loc = makeLocation(AST.getASTContext(),
Expand Down Expand Up @@ -366,17 +365,16 @@ std::vector<DocumentHighlight> findDocumentHighlights(ParsedAST &AST,
auto References = findRefs(
getDeclAtPosition(AST,
SM.getMacroArgExpandedLocation(getBeginningOfIdentifier(
Pos, SM, AST.getASTContext().getLangOpts())),
Pos, SM, AST.getLangOpts())),
Relations),
AST);

// FIXME: we may get multiple DocumentHighlights with the same location and
// different kinds, deduplicate them.
std::vector<DocumentHighlight> Result;
for (const auto &Ref : References) {
if (auto Range =
getTokenRange(AST.getASTContext().getSourceManager(),
AST.getASTContext().getLangOpts(), Ref.Loc)) {
if (auto Range = getTokenRange(AST.getASTContext().getSourceManager(),
AST.getLangOpts(), Ref.Loc)) {
DocumentHighlight DH;
DH.range = *Range;
if (Ref.Role & index::SymbolRoleSet(index::SymbolRole::Write))
Expand Down Expand Up @@ -404,7 +402,7 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
return Results;
}
auto Loc = SM.getMacroArgExpandedLocation(
getBeginningOfIdentifier(Pos, SM, AST.getASTContext().getLangOpts()));
getBeginningOfIdentifier(Pos, SM, AST.getLangOpts()));
// TODO: should we handle macros, too?
// We also show references to the targets of using-decls, so we include
// DeclRelation::Underlying.
Expand All @@ -424,8 +422,7 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
}),
MainFileRefs.end());
for (const auto &Ref : MainFileRefs) {
if (auto Range =
getTokenRange(SM, AST.getASTContext().getLangOpts(), Ref.Loc)) {
if (auto Range = getTokenRange(SM, AST.getLangOpts(), Ref.Loc)) {
Location Result;
Result.range = *Range;
Result.uri = URIForFile::canonicalize(*MainFilePath, *MainFilePath);
Expand Down Expand Up @@ -470,7 +467,7 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
std::vector<SymbolDetails> getSymbolInfo(ParsedAST &AST, Position Pos) {
const SourceManager &SM = AST.getSourceManager();
auto Loc = SM.getMacroArgExpandedLocation(
getBeginningOfIdentifier(Pos, SM, AST.getASTContext().getLangOpts()));
getBeginningOfIdentifier(Pos, SM, AST.getLangOpts()));

std::vector<SymbolDetails> Results;

Expand Down Expand Up @@ -646,7 +643,7 @@ static void fillSuperTypes(const CXXRecordDecl &CXXRD, ASTContext &ASTCtx,
const CXXRecordDecl *findRecordTypeAt(ParsedAST &AST, Position Pos) {
const SourceManager &SM = AST.getSourceManager();
SourceLocation SourceLocationBeg = SM.getMacroArgExpandedLocation(
getBeginningOfIdentifier(Pos, SM, AST.getASTContext().getLangOpts()));
getBeginningOfIdentifier(Pos, SM, AST.getLangOpts()));
DeclRelationSet Relations =
DeclRelation::TemplatePattern | DeclRelation::Underlying;
auto Decls = getDeclAtPosition(AST, SourceLocationBeg, Relations);
Expand Down
77 changes: 48 additions & 29 deletions clang-tools-extra/clangd/index/SymbolCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "SourceCode.h"
#include "SymbolLocation.h"
#include "URI.h"
#include "index/SymbolID.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclBase.h"
#include "clang/AST/DeclCXX.h"
Expand Down Expand Up @@ -345,43 +346,52 @@ bool SymbolCollector::handleMacroOccurence(const IdentifierInfo *Name,
const MacroInfo *MI,
index::SymbolRoleSet Roles,
SourceLocation Loc) {
if (!Opts.CollectMacro)
return true;
assert(PP.get());

const auto &SM = PP->getSourceManager();
auto DefLoc = MI->getDefinitionLoc();
auto SpellingLoc = SM.getSpellingLoc(Loc);
bool IsMainFileSymbol = SM.isInMainFile(SM.getExpansionLoc(DefLoc));

// Builtin macros don't have useful locations and aren't needed in completion.
if (MI->isBuiltinMacro())
return true;

// Skip main-file symbols if we are not collecting them.
bool IsMainFileSymbol = SM.isInMainFile(SM.getExpansionLoc(DefLoc));
if (IsMainFileSymbol && !Opts.CollectMainFileSymbols)
return false;

// Also avoid storing predefined macros like __DBL_MIN__.
if (SM.isWrittenInBuiltinFile(DefLoc))
return true;

auto ID = getSymbolID(Name->getName(), MI, SM);
if (!ID)
return true;

// Do not store references to main-file macros.
if ((static_cast<unsigned>(Opts.RefFilter) & Roles) && !IsMainFileSymbol &&
(Opts.RefsInHeaders || SM.getFileID(SpellingLoc) == SM.getMainFileID()))
MacroRefs[*ID].push_back({Loc, Roles});

// Collect symbols.
if (!Opts.CollectMacro)
return true;

// Skip main-file macros if we are not collecting them.
if (IsMainFileSymbol && !Opts.CollectMainFileSymbols)
return false;

// Mark the macro as referenced if this is a reference coming from the main
// file. The macro may not be an interesting symbol, but it's cheaper to check
// at the end.
if (Opts.CountReferences &&
(Roles & static_cast<unsigned>(index::SymbolRole::Reference)) &&
SM.getFileID(SM.getSpellingLoc(Loc)) == SM.getMainFileID())
SM.getFileID(SpellingLoc) == SM.getMainFileID())
ReferencedMacros.insert(Name);

// Don't continue indexing if this is a mere reference.
// FIXME: remove macro with ID if it is undefined.
if (!(Roles & static_cast<unsigned>(index::SymbolRole::Declaration) ||
Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
return true;

auto ID = getSymbolID(Name->getName(), MI, SM);
if (!ID)
return true;

// Only collect one instance in case there are multiple.
if (Symbols.find(*ID) != nullptr)
return true;
Expand Down Expand Up @@ -485,10 +495,10 @@ void SymbolCollector::finish() {
IncRef(*ID);
}
}

// Fill in IncludeHeaders.
// We delay this until end of TU so header guards are all resolved.
// Symbols in slabs aren' mutable, so insert() has to walk all the strings :-(
// Symbols in slabs aren' mutable, so insert() has to walk all the strings
// :-(
llvm::SmallString<256> QName;
for (const auto &Entry : IncludeFiles)
if (const Symbol *S = Symbols.find(Entry.first)) {
Expand Down Expand Up @@ -518,25 +528,34 @@ void SymbolCollector::finish() {
}
return Found->second;
};
auto CollectRef =
[&](SymbolID ID,
const std::pair<SourceLocation, index::SymbolRoleSet> &LocAndRole) {
auto FileID = SM.getFileID(LocAndRole.first);
// FIXME: use the result to filter out references.
shouldIndexFile(FileID);
if (auto FileURI = GetURI(FileID)) {
auto Range =
getTokenRange(LocAndRole.first, SM, ASTCtx->getLangOpts());
Ref R;
R.Location.Start = Range.first;
R.Location.End = Range.second;
R.Location.FileURI = FileURI->c_str();
R.Kind = toRefKind(LocAndRole.second);
Refs.insert(ID, R);
}
};
// Populate Refs slab from MacroRefs.
for (const auto &IDAndRefs : MacroRefs) {
for (const auto &LocAndRole : IDAndRefs.second)
CollectRef(IDAndRefs.first, LocAndRole);
}
// Populate Refs slab from DeclRefs.
if (auto MainFileURI = GetURI(SM.getMainFileID())) {
for (const auto &It : DeclRefs) {
if (auto ID = getSymbolID(It.first)) {
for (const auto &LocAndRole : It.second) {
auto FileID = SM.getFileID(LocAndRole.first);
// FIXME: use the result to filter out references.
shouldIndexFile(FileID);
if (auto FileURI = GetURI(FileID)) {
auto Range =
getTokenRange(LocAndRole.first, SM, ASTCtx->getLangOpts());
Ref R;
R.Location.Start = Range.first;
R.Location.End = Range.second;
R.Location.FileURI = FileURI->c_str();
R.Kind = toRefKind(LocAndRole.second);
Refs.insert(*ID, R);
}
}
for (const auto &LocAndRole : It.second)
CollectRef(*ID, LocAndRole);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions clang-tools-extra/clangd/index/SymbolCollector.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,12 @@ class SymbolCollector : public index::IndexDataConsumer {
std::shared_ptr<GlobalCodeCompletionAllocator> CompletionAllocator;
std::unique_ptr<CodeCompletionTUInfo> CompletionTUInfo;
Options Opts;
using DeclRef = std::pair<SourceLocation, index::SymbolRoleSet>;
using SymbolRef = std::pair<SourceLocation, index::SymbolRoleSet>;
// Symbols referenced from the current TU, flushed on finish().
llvm::DenseSet<const NamedDecl *> ReferencedDecls;
llvm::DenseSet<const IdentifierInfo *> ReferencedMacros;
llvm::DenseMap<const NamedDecl *, std::vector<DeclRef>> DeclRefs;
llvm::DenseMap<const NamedDecl *, std::vector<SymbolRef>> DeclRefs;
llvm::DenseMap<SymbolID, std::vector<SymbolRef>> MacroRefs;
// Maps canonical declaration provided by clang to canonical declaration for
// an index symbol, if clangd prefers a different declaration than that
// provided by clang. For example, friend declaration might be considered
Expand Down
5 changes: 2 additions & 3 deletions clang-tools-extra/clangd/refactor/Rename.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,8 @@ llvm::Expected<FileEdits> rename(const RenameInputs &RInputs) {

return (*Content)->getBuffer().str();
};
SourceLocation SourceLocationBeg =
SM.getMacroArgExpandedLocation(getBeginningOfIdentifier(
RInputs.Pos, SM, AST.getASTContext().getLangOpts()));
SourceLocation SourceLocationBeg = SM.getMacroArgExpandedLocation(
getBeginningOfIdentifier(RInputs.Pos, SM, AST.getLangOpts()));
// FIXME: Renaming macros is not supported yet, the macro-handling code should
// be moved to rename tooling library.
if (locateMacroAt(SourceLocationBeg, AST.getPreprocessor()))
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clangd/refactor/tweaks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ add_clang_library(clangDaemonTweaks OBJECT
ExpandMacro.cpp
ExtractFunction.cpp
ExtractVariable.cpp
ObjCLocalizeStringLiteral.cpp
RawStringLiteral.cpp
RemoveUsingNamespace.cpp
SwapIfBranches.cpp
Expand Down
7 changes: 3 additions & 4 deletions clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class DefineOutline : public Tweak {
// FIXME: We might want to consider moving method definitions below class
// definition even if we are inside a source file.
if (!isHeaderFile(Sel.AST.getSourceManager().getFilename(Sel.Cursor),
Sel.AST.getASTContext().getLangOpts()))
Sel.AST.getLangOpts()))
return false;

Source = getSelectedFunction(Sel.ASTSelection.commonAncestor());
Expand Down Expand Up @@ -306,9 +306,8 @@ class DefineOutline : public Tweak {
// FIXME: We should also get rid of inline qualifier.
const tooling::Replacement DeleteFuncBody(
Sel.AST.getSourceManager(),
CharSourceRange::getTokenRange(
*toHalfOpenFileRange(SM, Sel.AST.getASTContext().getLangOpts(),
Source->getBody()->getSourceRange())),
CharSourceRange::getTokenRange(*toHalfOpenFileRange(
SM, Sel.AST.getLangOpts(), Source->getBody()->getSourceRange())),
";");
auto HeaderFE = Effect::fileEdit(SM, SM.getMainFileID(),
tooling::Replacements(DeleteFuncBody));
Expand Down
4 changes: 2 additions & 2 deletions clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ tooling::Replacement createFunctionDefinition(const NewFunction &ExtractedFunc,
bool ExtractFunction::prepare(const Selection &Inputs) {
const Node *CommonAnc = Inputs.ASTSelection.commonAncestor();
const SourceManager &SM = Inputs.AST.getSourceManager();
const LangOptions &LangOpts = Inputs.AST.getASTContext().getLangOpts();
const LangOptions &LangOpts = Inputs.AST.getLangOpts();
if (auto MaybeExtZone = findExtractionZone(CommonAnc, SM, LangOpts)) {
ExtZone = std::move(*MaybeExtZone);
return true;
Expand All @@ -655,7 +655,7 @@ bool ExtractFunction::prepare(const Selection &Inputs) {

Expected<Tweak::Effect> ExtractFunction::apply(const Selection &Inputs) {
const SourceManager &SM = Inputs.AST.getSourceManager();
const LangOptions &LangOpts = Inputs.AST.getASTContext().getLangOpts();
const LangOptions &LangOpts = Inputs.AST.getLangOpts();
auto ExtractedFunc = getExtractedFunction(ExtZone, SM, LangOpts);
// FIXME: Add more types of errors.
if (!ExtractedFunc)
Expand Down
Loading

0 comments on commit 0f31e70

Please sign in to comment.