Skip to content

Commit

Permalink
[clangd] Remove 'using namespace llvm' from .cpp files. NFC
Browse files Browse the repository at this point in the history
The new guideline is to qualify with 'llvm::' explicitly both in
'.h' and '.cpp' files. This simplifies moving the code between
header and source files and is easier to keep consistent.

llvm-svn: 350531
  • Loading branch information
ilya-biryukov committed Jan 7, 2019
1 parent 25d3de8 commit f2001aa
Showing 80 changed files with 1,822 additions and 1,745 deletions.
18 changes: 9 additions & 9 deletions clang-tools-extra/clangd/AST.cpp
Original file line number Diff line number Diff line change
@@ -18,7 +18,6 @@
#include "llvm/Support/Casting.h"
#include "llvm/Support/ScopedPrinter.h"

using namespace llvm;
namespace clang {
namespace clangd {

@@ -35,8 +34,8 @@ bool isSpelledInSourceCode(const Decl *D) {
// macros, we should use the location where the whole definition occurs.
if (Loc.isMacroID()) {
std::string PrintLoc = SM.getSpellingLoc(Loc).printToString(SM);
if (StringRef(PrintLoc).startswith("<scratch") ||
StringRef(PrintLoc).startswith("<command line>"))
if (llvm::StringRef(PrintLoc).startswith("<scratch") ||
llvm::StringRef(PrintLoc).startswith("<command line>"))
return false;
}
return true;
@@ -54,7 +53,7 @@ SourceLocation findNameLoc(const clang::Decl *D) {

std::string printQualifiedName(const NamedDecl &ND) {
std::string QName;
raw_string_ostream OS(QName);
llvm::raw_string_ostream OS(QName);
PrintingPolicy Policy(ND.getASTContext().getLangOpts());
// Note that inline namespaces are treated as transparent scopes. This
// reflects the way they're most commonly used for lookup. Ideally we'd
@@ -115,18 +114,19 @@ std::string printNamespaceScope(const DeclContext &DC) {
return "";
}

Optional<SymbolID> getSymbolID(const Decl *D) {
SmallString<128> USR;
llvm::Optional<SymbolID> getSymbolID(const Decl *D) {
llvm::SmallString<128> USR;
if (index::generateUSRForDecl(D, USR))
return None;
return SymbolID(USR);
}

Optional<SymbolID> getSymbolID(const IdentifierInfo &II, const MacroInfo *MI,
const SourceManager &SM) {
llvm::Optional<SymbolID> getSymbolID(const IdentifierInfo &II,
const MacroInfo *MI,
const SourceManager &SM) {
if (MI == nullptr)
return None;
SmallString<128> USR;
llvm::SmallString<128> USR;
if (index::generateUSRForMacro(II.getName(), MI->getDefinitionLoc(), SM, USR))
return None;
return SymbolID(USR);
190 changes: 96 additions & 94 deletions clang-tools-extra/clangd/ClangdLSPServer.cpp

Large diffs are not rendered by default.

98 changes: 50 additions & 48 deletions clang-tools-extra/clangd/ClangdServer.cpp
Original file line number Diff line number Diff line change
@@ -34,7 +34,6 @@
#include <future>
#include <mutex>

using namespace llvm;
namespace clang {
namespace clangd {
namespace {
@@ -47,7 +46,7 @@ std::string getStandardResourceDir() {
class RefactoringResultCollector final
: public tooling::RefactoringResultConsumer {
public:
void handleError(Error Err) override {
void handleError(llvm::Error Err) override {
assert(!Result.hasValue());
// FIXME: figure out a way to return better message for DiagnosticError.
// clangd uses llvm::toString to convert the Err to string, however, for
@@ -63,7 +62,7 @@ class RefactoringResultCollector final
Result = std::move(SourceReplacements);
}

Optional<Expected<tooling::AtomicChanges>> Result;
llvm::Optional<llvm::Expected<tooling::AtomicChanges>> Result;
};

// Update the FileIndex with new ASTs and plumb the diagnostics responses.
@@ -147,7 +146,7 @@ ClangdServer::ClangdServer(const GlobalCompilationDatabase &CDB,
AddIndex(DynamicIdx.get());
}

void ClangdServer::addDocument(PathRef File, StringRef Contents,
void ClangdServer::addDocument(PathRef File, llvm::StringRef Contents,
WantDiagnostics WantDiags) {
// FIXME: some build systems like Bazel will take time to preparing
// environment to build the file, it would be nice if we could emit a
@@ -175,13 +174,13 @@ void ClangdServer::codeComplete(PathRef File, Position Pos,

auto Task = [PCHs, Pos, FS, CodeCompleteOpts,
this](Path File, Callback<CodeCompleteResult> CB,
Expected<InputsAndPreamble> IP) {
llvm::Expected<InputsAndPreamble> IP) {
if (!IP)
return CB(IP.takeError());
if (isCancelled())
return CB(make_error<CancelledError>());
return CB(llvm::make_error<CancelledError>());

Optional<SpeculativeFuzzyFind> SpecFuzzyFind;
llvm::Optional<SpeculativeFuzzyFind> SpecFuzzyFind;
if (CodeCompleteOpts.Index && CodeCompleteOpts.SpeculativeIndexRequest) {
SpecFuzzyFind.emplace();
{
@@ -222,7 +221,7 @@ void ClangdServer::signatureHelp(PathRef File, Position Pos,
auto FS = FSProvider.getFileSystem();
auto *Index = this->Index;
auto Action = [Pos, FS, PCHs, Index](Path File, Callback<SignatureHelp> CB,
Expected<InputsAndPreamble> IP) {
llvm::Expected<InputsAndPreamble> IP) {
if (!IP)
return CB(IP.takeError());

@@ -239,43 +238,44 @@ void ClangdServer::signatureHelp(PathRef File, Position Pos,
Bind(Action, File.str(), std::move(CB)));
}

Expected<tooling::Replacements>
ClangdServer::formatRange(StringRef Code, PathRef File, Range Rng) {
Expected<size_t> Begin = positionToOffset(Code, Rng.start);
llvm::Expected<tooling::Replacements>
ClangdServer::formatRange(llvm::StringRef Code, PathRef File, Range Rng) {
llvm::Expected<size_t> Begin = positionToOffset(Code, Rng.start);
if (!Begin)
return Begin.takeError();
Expected<size_t> End = positionToOffset(Code, Rng.end);
llvm::Expected<size_t> End = positionToOffset(Code, Rng.end);
if (!End)
return End.takeError();
return formatCode(Code, File, {tooling::Range(*Begin, *End - *Begin)});
}

Expected<tooling::Replacements> ClangdServer::formatFile(StringRef Code,
PathRef File) {
llvm::Expected<tooling::Replacements>
ClangdServer::formatFile(llvm::StringRef Code, PathRef File) {
// Format everything.
return formatCode(Code, File, {tooling::Range(0, Code.size())});
}

Expected<tooling::Replacements>
ClangdServer::formatOnType(StringRef Code, PathRef File, Position Pos) {
llvm::Expected<tooling::Replacements>
ClangdServer::formatOnType(llvm::StringRef Code, PathRef File, Position Pos) {
// Look for the previous opening brace from the character position and
// format starting from there.
Expected<size_t> CursorPos = positionToOffset(Code, Pos);
llvm::Expected<size_t> CursorPos = positionToOffset(Code, Pos);
if (!CursorPos)
return CursorPos.takeError();
size_t PreviousLBracePos = StringRef(Code).find_last_of('{', *CursorPos);
if (PreviousLBracePos == StringRef::npos)
size_t PreviousLBracePos =
llvm::StringRef(Code).find_last_of('{', *CursorPos);
if (PreviousLBracePos == llvm::StringRef::npos)
PreviousLBracePos = *CursorPos;
size_t Len = *CursorPos - PreviousLBracePos;

return formatCode(Code, File, {tooling::Range(PreviousLBracePos, Len)});
}

void ClangdServer::rename(PathRef File, Position Pos, StringRef NewName,
void ClangdServer::rename(PathRef File, Position Pos, llvm::StringRef NewName,
Callback<std::vector<tooling::Replacement>> CB) {
auto Action = [Pos](Path File, std::string NewName,
Callback<std::vector<tooling::Replacement>> CB,
Expected<InputsAndAST> InpAST) {
llvm::Expected<InputsAndAST> InpAST) {
if (!InpAST)
return CB(InpAST.takeError());
auto &AST = InpAST->AST;
@@ -323,15 +323,16 @@ void ClangdServer::rename(PathRef File, Position Pos, StringRef NewName,
}

void ClangdServer::dumpAST(PathRef File,
unique_function<void(std::string)> Callback) {
auto Action = [](decltype(Callback) Callback, Expected<InputsAndAST> InpAST) {
llvm::unique_function<void(std::string)> Callback) {
auto Action = [](decltype(Callback) Callback,
llvm::Expected<InputsAndAST> InpAST) {
if (!InpAST) {
llvm::consumeError(InpAST.takeError());
return Callback("<no-ast>");
}
std::string Result;

raw_string_ostream ResultOS(Result);
llvm::raw_string_ostream ResultOS(Result);
clangd::dumpAST(InpAST->AST, ResultOS);
ResultOS.flush();

@@ -344,7 +345,7 @@ void ClangdServer::dumpAST(PathRef File,
void ClangdServer::findDefinitions(PathRef File, Position Pos,
Callback<std::vector<Location>> CB) {
auto Action = [Pos, this](Callback<std::vector<Location>> CB,
Expected<InputsAndAST> InpAST) {
llvm::Expected<InputsAndAST> InpAST) {
if (!InpAST)
return CB(InpAST.takeError());
CB(clangd::findDefinitions(InpAST->AST, Pos, Index));
@@ -353,13 +354,13 @@ void ClangdServer::findDefinitions(PathRef File, Position Pos,
WorkScheduler.runWithAST("Definitions", File, Bind(Action, std::move(CB)));
}

Optional<Path> ClangdServer::switchSourceHeader(PathRef Path) {
llvm::Optional<Path> ClangdServer::switchSourceHeader(PathRef Path) {

StringRef SourceExtensions[] = {".cpp", ".c", ".cc", ".cxx",
".c++", ".m", ".mm"};
StringRef HeaderExtensions[] = {".h", ".hh", ".hpp", ".hxx", ".inc"};
llvm::StringRef SourceExtensions[] = {".cpp", ".c", ".cc", ".cxx",
".c++", ".m", ".mm"};
llvm::StringRef HeaderExtensions[] = {".h", ".hh", ".hpp", ".hxx", ".inc"};

StringRef PathExt = sys::path::extension(Path);
llvm::StringRef PathExt = llvm::sys::path::extension(Path);

// Lookup in a list of known extensions.
auto SourceIter =
@@ -381,38 +382,38 @@ Optional<Path> ClangdServer::switchSourceHeader(PathRef Path) {

// Array to lookup extensions for the switch. An opposite of where original
// extension was found.
ArrayRef<StringRef> NewExts;
llvm::ArrayRef<llvm::StringRef> NewExts;
if (IsSource)
NewExts = HeaderExtensions;
else
NewExts = SourceExtensions;

// Storage for the new path.
SmallString<128> NewPath = StringRef(Path);
llvm::SmallString<128> NewPath = llvm::StringRef(Path);

// Instance of vfs::FileSystem, used for file existence checks.
auto FS = FSProvider.getFileSystem();

// Loop through switched extension candidates.
for (StringRef NewExt : NewExts) {
sys::path::replace_extension(NewPath, NewExt);
for (llvm::StringRef NewExt : NewExts) {
llvm::sys::path::replace_extension(NewPath, NewExt);
if (FS->exists(NewPath))
return NewPath.str().str(); // First str() to convert from SmallString to
// StringRef, second to convert from StringRef
// to std::string

// Also check NewExt in upper-case, just in case.
sys::path::replace_extension(NewPath, NewExt.upper());
llvm::sys::path::replace_extension(NewPath, NewExt.upper());
if (FS->exists(NewPath))
return NewPath.str().str();
}

return None;
}

Expected<tooling::Replacements>
ClangdServer::formatCode(StringRef Code, PathRef File,
ArrayRef<tooling::Range> Ranges) {
llvm::Expected<tooling::Replacements>
ClangdServer::formatCode(llvm::StringRef Code, PathRef File,
llvm::ArrayRef<tooling::Range> Ranges) {
// Call clang-format.
auto FS = FSProvider.getFileSystem();
auto Style = format::getStyle(format::DefaultFormatStyle, File,
@@ -435,7 +436,7 @@ ClangdServer::formatCode(StringRef Code, PathRef File,
void ClangdServer::findDocumentHighlights(
PathRef File, Position Pos, Callback<std::vector<DocumentHighlight>> CB) {
auto Action = [Pos](Callback<std::vector<DocumentHighlight>> CB,
Expected<InputsAndAST> InpAST) {
llvm::Expected<InputsAndAST> InpAST) {
if (!InpAST)
return CB(InpAST.takeError());
CB(clangd::findDocumentHighlights(InpAST->AST, Pos));
@@ -445,9 +446,9 @@ void ClangdServer::findDocumentHighlights(
}

void ClangdServer::findHover(PathRef File, Position Pos,
Callback<Optional<Hover>> CB) {
auto Action = [Pos](Callback<Optional<Hover>> CB,
Expected<InputsAndAST> InpAST) {
Callback<llvm::Optional<Hover>> CB) {
auto Action = [Pos](Callback<llvm::Optional<Hover>> CB,
llvm::Expected<InputsAndAST> InpAST) {
if (!InpAST)
return CB(InpAST.takeError());
CB(clangd::getHover(InpAST->AST, Pos));
@@ -458,7 +459,7 @@ void ClangdServer::findHover(PathRef File, Position Pos,

tooling::CompileCommand ClangdServer::getCompileCommand(PathRef File) {
trace::Span Span("GetCompileCommand");
Optional<tooling::CompileCommand> C = CDB.getCompileCommand(File);
llvm::Optional<tooling::CompileCommand> C = CDB.getCompileCommand(File);
if (!C) // FIXME: Suppress diagnostics? Let the user know?
C = CDB.getFallbackCommand(File);

@@ -474,7 +475,8 @@ void ClangdServer::onFileEvent(const DidChangeWatchedFilesParams &Params) {
}

void ClangdServer::workspaceSymbols(
StringRef Query, int Limit, Callback<std::vector<SymbolInformation>> CB) {
llvm::StringRef Query, int Limit,
Callback<std::vector<SymbolInformation>> CB) {
std::string QueryCopy = Query;
WorkScheduler.run(
"getWorkspaceSymbols",
@@ -486,7 +488,7 @@ void ClangdServer::workspaceSymbols(
std::move(CB)));
}

void ClangdServer::documentSymbols(StringRef File,
void ClangdServer::documentSymbols(llvm::StringRef File,
Callback<std::vector<DocumentSymbol>> CB) {
auto Action = [](Callback<std::vector<DocumentSymbol>> CB,
llvm::Expected<InputsAndAST> InpAST) {
@@ -501,7 +503,7 @@ void ClangdServer::documentSymbols(StringRef File,
void ClangdServer::findReferences(PathRef File, Position Pos,
Callback<std::vector<Location>> CB) {
auto Action = [Pos, this](Callback<std::vector<Location>> CB,
Expected<InputsAndAST> InpAST) {
llvm::Expected<InputsAndAST> InpAST) {
if (!InpAST)
return CB(InpAST.takeError());
CB(clangd::findReferences(InpAST->AST, Pos, Index));
@@ -513,7 +515,7 @@ void ClangdServer::findReferences(PathRef File, Position Pos,
void ClangdServer::symbolInfo(PathRef File, Position Pos,
Callback<std::vector<SymbolDetails>> CB) {
auto Action = [Pos](Callback<std::vector<SymbolDetails>> CB,
Expected<InputsAndAST> InpAST) {
llvm::Expected<InputsAndAST> InpAST) {
if (!InpAST)
return CB(InpAST.takeError());
CB(clangd::getSymbolInfo(InpAST->AST, Pos));
@@ -528,7 +530,7 @@ ClangdServer::getUsedBytesPerFile() const {
}

LLVM_NODISCARD bool
ClangdServer::blockUntilIdleForTest(Optional<double> TimeoutSeconds) {
ClangdServer::blockUntilIdleForTest(llvm::Optional<double> TimeoutSeconds) {
return WorkScheduler.blockUntilIdle(timeoutSeconds(TimeoutSeconds)) &&
(!BackgroundIdx ||
BackgroundIdx->blockUntilIdleForTest(TimeoutSeconds));
Loading

0 comments on commit f2001aa

Please sign in to comment.