Skip to content

Commit

Permalink
Merged master:5d1f8395be94 into amd-gfx:0b4255fb2027
Browse files Browse the repository at this point in the history
Local branch amd-gfx 0b4255f Merge remote-tracking branch 'llvm.org/master' into merge-candidate
Remote branch master 5d1f839 [AIX] Enable large code model when building with clang
  • Loading branch information
Sw authored and Sw committed Sep 18, 2020
2 parents 0b4255f + 5d1f839 commit 606e097
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 1 deletion.
8 changes: 8 additions & 0 deletions clang-tools-extra/clangd/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ struct Config {
/// Whether this TU should be indexed.
BackgroundPolicy Background = BackgroundPolicy::Build;
} Index;

/// Style of the codebase.
struct {
// Namespaces that should always be fully qualified, meaning no "using"
// declarations, always spell out the whole name (with or without leading
// ::). All nested namespaces are affected as well.
std::vector<std::string> FullyQualifiedNamespaces;
} Style;
};

} // namespace clangd
Expand Down
19 changes: 19 additions & 0 deletions clang-tools-extra/clangd/ConfigCompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,25 @@ struct FragmentCompiler {
}
}

void compile(Fragment::StyleBlock &&F) {
if (!F.FullyQualifiedNamespaces.empty()) {
std::vector<std::string> FullyQualifiedNamespaces;
for (auto &N : F.FullyQualifiedNamespaces) {
// Normalize the data by dropping both leading and trailing ::
StringRef Namespace(*N);
Namespace.consume_front("::");
Namespace.consume_back("::");
FullyQualifiedNamespaces.push_back(Namespace.str());
}
Out.Apply.push_back([FullyQualifiedNamespaces(
std::move(FullyQualifiedNamespaces))](Config &C) {
C.Style.FullyQualifiedNamespaces.insert(
C.Style.FullyQualifiedNamespaces.begin(),
FullyQualifiedNamespaces.begin(), FullyQualifiedNamespaces.end());
});
}
}

constexpr static llvm::SourceMgr::DiagKind Error = llvm::SourceMgr::DK_Error;
constexpr static llvm::SourceMgr::DiagKind Warning =
llvm::SourceMgr::DK_Warning;
Expand Down
10 changes: 10 additions & 0 deletions clang-tools-extra/clangd/ConfigFragment.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ struct Fragment {
llvm::Optional<Located<std::string>> Background;
};
IndexBlock Index;

// Describes the style of the codebase, beyond formatting.
struct StyleBlock {
// Namespaces that should always be fully qualified, meaning no "using"
// declarations, always spell out the whole name (with or without leading
// ::). All nested namespaces are affected as well.
// Affects availability of the AddUsing tweak.
std::vector<Located<std::string>> FullyQualifiedNamespaces;
};
StyleBlock Style;
};

} // namespace config
Expand Down
10 changes: 10 additions & 0 deletions clang-tools-extra/clangd/ConfigYAML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Parser {
Dict.handle("If", [&](Node &N) { parse(F.If, N); });
Dict.handle("CompileFlags", [&](Node &N) { parse(F.CompileFlags, N); });
Dict.handle("Index", [&](Node &N) { parse(F.Index, N); });
Dict.handle("Style", [&](Node &N) { parse(F.Style, N); });
Dict.parse(N);
return !(N.failed() || HadError);
}
Expand Down Expand Up @@ -72,6 +73,15 @@ class Parser {
Dict.parse(N);
}

void parse(Fragment::StyleBlock &F, Node &N) {
DictParser Dict("Style", this);
Dict.handle("FullyQualifiedNamespaces", [&](Node &N) {
if (auto Values = scalarValues(N))
F.FullyQualifiedNamespaces = std::move(*Values);
});
Dict.parse(N);
}

void parse(Fragment::IndexBlock &F, Node &N) {
DictParser Dict("Index", this);
Dict.handle("Background",
Expand Down
17 changes: 17 additions & 0 deletions clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "AST.h"
#include "Config.h"
#include "FindTarget.h"
#include "refactor/Tweak.h"
#include "support/Logger.h"
Expand Down Expand Up @@ -190,6 +191,19 @@ findInsertionPoint(const Tweak::Selection &Inputs,
return Out;
}

bool isNamespaceForbidden(const Tweak::Selection &Inputs,
const NestedNameSpecifier &Namespace) {
std::string NamespaceStr = printNamespaceScope(*Namespace.getAsNamespace());

for (StringRef Banned : Config::current().Style.FullyQualifiedNamespaces) {
StringRef PrefixMatch = NamespaceStr;
if (PrefixMatch.consume_front(Banned) && PrefixMatch.consume_front("::"))
return true;
}

return false;
}

bool AddUsing::prepare(const Selection &Inputs) {
auto &SM = Inputs.AST->getSourceManager();

Expand Down Expand Up @@ -248,6 +262,9 @@ bool AddUsing::prepare(const Selection &Inputs) {
return false;
}

if (isNamespaceForbidden(Inputs, *QualifierToRemove.getNestedNameSpecifier()))
return false;

// Macros are difficult. We only want to offer code action when what's spelled
// under the cursor is a namespace qualifier. If it's a macro that expands to
// a qualifier, user would not know what code action will actually change.
Expand Down
11 changes: 11 additions & 0 deletions clang-tools-extra/clangd/unittests/TweakTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "Annotations.h"
#include "Config.h"
#include "SourceCode.h"
#include "TestFS.h"
#include "TestTU.h"
Expand Down Expand Up @@ -2471,8 +2472,14 @@ TEST_F(DefineOutlineTest, FailsMacroSpecifier) {

TWEAK_TEST(AddUsing);
TEST_F(AddUsingTest, Prepare) {
Config Cfg;
Cfg.Style.FullyQualifiedNamespaces.push_back("ban");
WithContextValue WithConfig(Config::Key, std::move(Cfg));

const std::string Header = R"cpp(
#define NS(name) one::two::name
namespace ban { void foo() {} }
namespace banana { void foo() {} }
namespace one {
void oo() {}
template<typename TT> class tt {};
Expand Down Expand Up @@ -2506,6 +2513,10 @@ class cc {
// Test that we don't crash or misbehave on unnamed DeclRefExpr.
EXPECT_UNAVAILABLE(Header +
"void fun() { one::two::cc() ^| one::two::cc(); }");
// Do not offer code action when operating on a banned namespace.
EXPECT_UNAVAILABLE(Header + "void fun() { ban::fo^o(); }");
EXPECT_UNAVAILABLE(Header + "void fun() { ::ban::fo^o(); }");
EXPECT_AVAILABLE(Header + "void fun() { banana::fo^o(); }");

// Check that we do not trigger in header files.
FileName = "test.h";
Expand Down
2 changes: 1 addition & 1 deletion llvm/cmake/modules/HandleLLVMOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ endif()

if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
# -fPIC does not enable the large code model for GCC on AIX but does for XL.
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
append("-mcmodel=large" CMAKE_CXX_FLAGS CMAKE_C_FLAGS)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "XL")
# XL generates a small number of relocations not of the large model, -bbigtoc is needed.
Expand Down

0 comments on commit 606e097

Please sign in to comment.