Skip to content

Commit

Permalink
Merge remote-tracking branch 'llvm/release/9.x' into rustc/9.0-2019-0…
Browse files Browse the repository at this point in the history
…7-12
  • Loading branch information
alexcrichton committed Jul 19, 2019
2 parents f6446fa + 1931d3c commit 93abf26
Show file tree
Hide file tree
Showing 1,288 changed files with 48,086 additions and 13,809 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ void AssertSideEffectCheck::check(const MatchFinder::MatchResult &Result) {
StringRef MacroName = Lexer::getImmediateMacroName(Loc, SM, LangOpts);

// Check if this macro is an assert.
if (std::find(AssertMacros.begin(), AssertMacros.end(), MacroName) !=
AssertMacros.end()) {
if (llvm::is_contained(AssertMacros, MacroName)) {
AssertMacroName = MacroName;
break;
}
Expand Down
17 changes: 11 additions & 6 deletions clang-tools-extra/clang-tidy/bugprone/BranchCloneCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,12 @@ void BranchCloneCheck::check(const MatchFinder::MatchResult &Result) {
// We report the first occurence only when we find the second one.
diag(Branches[i]->getBeginLoc(),
"repeated branch in conditional chain");
diag(Lexer::getLocForEndOfToken(Branches[i]->getEndLoc(), 0,
*Result.SourceManager, getLangOpts()),
"end of the original", DiagnosticIDs::Note);
SourceLocation End =
Lexer::getLocForEndOfToken(Branches[i]->getEndLoc(), 0,
*Result.SourceManager, getLangOpts());
if (End.isValid()) {
diag(End, "end of the original", DiagnosticIDs::Note);
}
}

diag(Branches[j]->getBeginLoc(), "clone %0 starts here",
Expand Down Expand Up @@ -208,10 +211,12 @@ void BranchCloneCheck::check(const MatchFinder::MatchResult &Result) {

if (EndLoc.isMacroID())
EndLoc = Context.getSourceManager().getExpansionLoc(EndLoc);
EndLoc = Lexer::getLocForEndOfToken(EndLoc, 0, *Result.SourceManager,
getLangOpts());

diag(Lexer::getLocForEndOfToken(EndLoc, 0, *Result.SourceManager,
getLangOpts()),
"last of these clones ends here", DiagnosticIDs::Note);
if (EndLoc.isValid()) {
diag(EndLoc, "last of these clones ends here", DiagnosticIDs::Note);
}
}
BeginCurrent = EndCurrent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void ForwardingReferenceOverloadCheck::check(
// template as the function parameter of that type. (This implies that type
// deduction will happen on the type.)
const TemplateParameterList *Params = FuncTemplate->getTemplateParameters();
if (std::find(Params->begin(), Params->end(), TypeParmDecl) == Params->end())
if (!llvm::is_contained(*Params, TypeParmDecl))
return;

// Every parameter after the first must have a default value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ void MultipleInheritanceCheck::registerMatchers(MatchFinder *Finder) {
return;

// Match declarations which have bases.
Finder->addMatcher(cxxRecordDecl(hasBases()).bind("decl"), this);
Finder->addMatcher(
cxxRecordDecl(allOf(hasBases(), isDefinition())).bind("decl"), this);
}

void MultipleInheritanceCheck::check(const MatchFinder::MatchResult &Result) {
Expand Down
7 changes: 2 additions & 5 deletions clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,8 @@ class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {
getOutermostMacroName(StartLoc, SM, Context.getLangOpts());

// Check to see if the user wants to replace the macro being expanded.
if (std::find(NullMacros.begin(), NullMacros.end(), OutermostMacroName) ==
NullMacros.end()) {
if (!llvm::is_contained(NullMacros, OutermostMacroName))
return skipSubTree();
}

StartLoc = SM.getFileLoc(StartLoc);
EndLoc = SM.getFileLoc(EndLoc);
Expand Down Expand Up @@ -327,8 +325,7 @@ class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {

StringRef Name =
Lexer::getImmediateMacroName(OldArgLoc, SM, Context.getLangOpts());
return std::find(NullMacros.begin(), NullMacros.end(), Name) !=
NullMacros.end();
return llvm::is_contained(NullMacros, Name);
}

MacroLoc = SM.getExpansionRange(ArgLoc).getBegin();
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clang-tidy/readability/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ add_clang_library(clangTidyReadabilityModule
BracesAroundStatementsCheck.cpp
ConstReturnTypeCheck.cpp
ContainerSizeEmptyCheck.cpp
ConvertMemberFunctionsToStatic.cpp
DeleteNullPointerCheck.cpp
DeletedDefaultCheck.cpp
ElseAfterReturnCheck.cpp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
//===--- ConvertMemberFunctionsToStatic.cpp - clang-tidy ------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "ConvertMemberFunctionsToStatic.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Basic/SourceLocation.h"

using namespace clang::ast_matchers;

namespace clang {
namespace tidy {
namespace readability {

AST_MATCHER(CXXMethodDecl, isStatic) { return Node.isStatic(); }

AST_MATCHER(CXXMethodDecl, hasTrivialBody) { return Node.hasTrivialBody(); }

AST_MATCHER(CXXMethodDecl, isOverloadedOperator) {
return Node.isOverloadedOperator();
}

AST_MATCHER(CXXRecordDecl, hasAnyDependentBases) {
return Node.hasAnyDependentBases();
}

AST_MATCHER(CXXMethodDecl, isTemplate) {
return Node.getTemplatedKind() != FunctionDecl::TK_NonTemplate;
}

AST_MATCHER(CXXMethodDecl, isDependentContext) {
return Node.isDependentContext();
}

AST_MATCHER(CXXMethodDecl, isInsideMacroDefinition) {
const ASTContext &Ctxt = Finder->getASTContext();
return clang::Lexer::makeFileCharRange(
clang::CharSourceRange::getCharRange(
Node.getTypeSourceInfo()->getTypeLoc().getSourceRange()),
Ctxt.getSourceManager(), Ctxt.getLangOpts())
.isInvalid();
}

AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl,
ast_matchers::internal::Matcher<CXXMethodDecl>, InnerMatcher) {
return InnerMatcher.matches(*Node.getCanonicalDecl(), Finder, Builder);
}

AST_MATCHER(CXXMethodDecl, usesThis) {
class FindUsageOfThis : public RecursiveASTVisitor<FindUsageOfThis> {
public:
bool Used = false;

bool VisitCXXThisExpr(const CXXThisExpr *E) {
Used = true;
return false; // Stop traversal.
}
} UsageOfThis;

// TraverseStmt does not modify its argument.
UsageOfThis.TraverseStmt(const_cast<Stmt *>(Node.getBody()));

return UsageOfThis.Used;
}

void ConvertMemberFunctionsToStatic::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
cxxMethodDecl(
isDefinition(), isUserProvided(),
unless(anyOf(
isExpansionInSystemHeader(), isVirtual(), isStatic(),
hasTrivialBody(), isOverloadedOperator(), cxxConstructorDecl(),
cxxDestructorDecl(), cxxConversionDecl(), isTemplate(),
isDependentContext(),
ofClass(anyOf(
isLambda(),
hasAnyDependentBases()) // Method might become virtual
// depending on template base class.
),
isInsideMacroDefinition(),
hasCanonicalDecl(isInsideMacroDefinition()), usesThis())))
.bind("x"),
this);
}

/// \brief Obtain the original source code text from a SourceRange.
static StringRef getStringFromRange(SourceManager &SourceMgr,
const LangOptions &LangOpts,
SourceRange Range) {
if (SourceMgr.getFileID(Range.getBegin()) !=
SourceMgr.getFileID(Range.getEnd()))
return {};

return Lexer::getSourceText(CharSourceRange(Range, true), SourceMgr,
LangOpts);
}

static SourceRange getLocationOfConst(const TypeSourceInfo *TSI,
SourceManager &SourceMgr,
const LangOptions &LangOpts) {
assert(TSI);
const auto FTL = TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>();
assert(FTL);

SourceRange Range{FTL.getRParenLoc().getLocWithOffset(1),
FTL.getLocalRangeEnd()};
// Inside Range, there might be other keywords and trailing return types.
// Find the exact position of "const".
StringRef Text = getStringFromRange(SourceMgr, LangOpts, Range);
size_t Offset = Text.find("const");
if (Offset == StringRef::npos)
return {};

SourceLocation Start = Range.getBegin().getLocWithOffset(Offset);
return {Start, Start.getLocWithOffset(strlen("const") - 1)};
}

void ConvertMemberFunctionsToStatic::check(
const MatchFinder::MatchResult &Result) {
const auto *Definition = Result.Nodes.getNodeAs<CXXMethodDecl>("x");

// TODO: For out-of-line declarations, don't modify the source if the header
// is excluded by the -header-filter option.
DiagnosticBuilder Diag =
diag(Definition->getLocation(), "method %0 can be made static")
<< Definition;

// TODO: Would need to remove those in a fix-it.
if (Definition->getMethodQualifiers().hasVolatile() ||
Definition->getMethodQualifiers().hasRestrict() ||
Definition->getRefQualifier() != RQ_None)
return;

const CXXMethodDecl *Declaration = Definition->getCanonicalDecl();

if (Definition->isConst()) {
// Make sure that we either remove 'const' on both declaration and
// definition or emit no fix-it at all.
SourceRange DefConst = getLocationOfConst(Definition->getTypeSourceInfo(),
*Result.SourceManager,
Result.Context->getLangOpts());

if (DefConst.isInvalid())
return;

if (Declaration != Definition) {
SourceRange DeclConst = getLocationOfConst(
Declaration->getTypeSourceInfo(), *Result.SourceManager,
Result.Context->getLangOpts());

if (DeclConst.isInvalid())
return;
Diag << FixItHint::CreateRemoval(DeclConst);
}

// Remove existing 'const' from both declaration and definition.
Diag << FixItHint::CreateRemoval(DefConst);
}
Diag << FixItHint::CreateInsertion(Declaration->getBeginLoc(), "static ");
}

} // namespace readability
} // namespace tidy
} // namespace clang
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//===--- ConvertMemberFunctionsToStatic.h - clang-tidy ----------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_CONVERTMEMFUNCTOSTATIC_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_CONVERTMEMFUNCTOSTATIC_H

#include "../ClangTidy.h"

namespace clang {
namespace tidy {
namespace readability {

/// This check finds C++ class methods than can be made static
/// because they don't use the 'this' pointer.
///
/// For the user-facing documentation see:
/// http://clang.llvm.org/extra/clang-tidy/checks/
/// readability-convert-member-functions-to-static.html
class ConvertMemberFunctionsToStatic : public ClangTidyCheck {
public:
ConvertMemberFunctionsToStatic(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
};

} // namespace readability
} // namespace tidy
} // namespace clang

#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_CONVERTMEMFUNCTOSTATIC_H
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "NamespaceCommentCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Lex/Lexer.h"
#include "llvm/ADT/StringExtras.h"

Expand Down Expand Up @@ -181,7 +182,13 @@ void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
? "anonymous namespace"
: ("namespace '" + NestedNamespaceName.str() + "'");

diag(AfterRBrace, Message)
// Place diagnostic at an old comment, or closing brace if we did not have it.
SourceLocation DiagLoc =
OldCommentRange.getBegin() != OldCommentRange.getEnd()
? OldCommentRange.getBegin()
: ND->getRBraceLoc();

diag(DiagLoc, Message)
<< NamespaceName
<< FixItHint::CreateReplacement(
CharSourceRange::getCharRange(OldCommentRange),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "BracesAroundStatementsCheck.h"
#include "ConstReturnTypeCheck.h"
#include "ContainerSizeEmptyCheck.h"
#include "ConvertMemberFunctionsToStatic.h"
#include "DeleteNullPointerCheck.h"
#include "DeletedDefaultCheck.h"
#include "ElseAfterReturnCheck.h"
Expand Down Expand Up @@ -57,6 +58,8 @@ class ReadabilityModule : public ClangTidyModule {
"readability-const-return-type");
CheckFactories.registerCheck<ContainerSizeEmptyCheck>(
"readability-container-size-empty");
CheckFactories.registerCheck<ConvertMemberFunctionsToStatic>(
"readability-convert-member-functions-to-static");
CheckFactories.registerCheck<DeleteNullPointerCheck>(
"readability-delete-null-pointer");
CheckFactories.registerCheck<DeletedDefaultCheck>(
Expand Down
8 changes: 8 additions & 0 deletions clang-tools-extra/clangd/ClangdLSPServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,13 @@ void ClangdLSPServer::onTypeHierarchy(
Params.resolve, Params.direction, std::move(Reply));
}

void ClangdLSPServer::onResolveTypeHierarchy(
const ResolveTypeHierarchyItemParams &Params,
Callback<Optional<TypeHierarchyItem>> Reply) {
Server->resolveTypeHierarchy(Params.item, Params.resolve, Params.direction,
std::move(Reply));
}

void ClangdLSPServer::applyConfiguration(
const ConfigurationSettings &Settings) {
// Per-file update to the compilation database.
Expand Down Expand Up @@ -1021,6 +1028,7 @@ ClangdLSPServer::ClangdLSPServer(
MsgHandler->bind("workspace/didChangeConfiguration", &ClangdLSPServer::onChangeConfiguration);
MsgHandler->bind("textDocument/symbolInfo", &ClangdLSPServer::onSymbolInfo);
MsgHandler->bind("textDocument/typeHierarchy", &ClangdLSPServer::onTypeHierarchy);
MsgHandler->bind("typeHierarchy/resolve", &ClangdLSPServer::onResolveTypeHierarchy);
// clang-format on
}

Expand Down
2 changes: 2 additions & 0 deletions clang-tools-extra/clangd/ClangdLSPServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class ClangdLSPServer : private DiagnosticsConsumer {
Callback<llvm::Optional<Hover>>);
void onTypeHierarchy(const TypeHierarchyParams &,
Callback<llvm::Optional<TypeHierarchyItem>>);
void onResolveTypeHierarchy(const ResolveTypeHierarchyItemParams &,
Callback<llvm::Optional<TypeHierarchyItem>>);
void onChangeConfiguration(const DidChangeConfigurationParams &);
void onSymbolInfo(const TextDocumentPositionParams &,
Callback<std::vector<SymbolDetails>>);
Expand Down
7 changes: 7 additions & 0 deletions clang-tools-extra/clangd/ClangdServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,13 @@ void ClangdServer::typeHierarchy(PathRef File, Position Pos, int Resolve,
WorkScheduler.runWithAST("Type Hierarchy", File, Bind(Action, std::move(CB)));
}

void ClangdServer::resolveTypeHierarchy(
TypeHierarchyItem Item, int Resolve, TypeHierarchyDirection Direction,
Callback<llvm::Optional<TypeHierarchyItem>> CB) {
clangd::resolveTypeHierarchy(Item, Resolve, Direction, Index);
CB(Item);
}

void ClangdServer::onFileEvent(const DidChangeWatchedFilesParams &Params) {
// FIXME: Do nothing for now. This will be used for indexing and potentially
// invalidating other caches.
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/clangd/ClangdServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ class ClangdServer {
TypeHierarchyDirection Direction,
Callback<llvm::Optional<TypeHierarchyItem>> CB);

/// Resolve type hierarchy item in the given direction.
void resolveTypeHierarchy(TypeHierarchyItem Item, int Resolve,
TypeHierarchyDirection Direction,
Callback<llvm::Optional<TypeHierarchyItem>> CB);

/// Retrieve the top symbols from the workspace matching a query.
void workspaceSymbols(StringRef Query, int Limit,
Callback<std::vector<SymbolInformation>> CB);
Expand Down
Loading

0 comments on commit 93abf26

Please sign in to comment.