From c008af646620f6718384c2cd95f58a7311fe10fb Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Sat, 20 Oct 2018 15:30:37 +0000 Subject: [clangd] Namespace style cleanup in cpp files. NFC. Standardize on the most common namespace setup in our *.cpp files: using namespace llvm; namespace clang { namespace clangd { void foo(StringRef) { ... } And remove redundant llvm:: qualifiers. (Except for cases like make_unique where this causes problems with std:: and ADL). This choice is pretty arbitrary, but some broad consistency is nice. This is going to conflict with everything. Sorry :-/ Squash the other configurations: A) using namespace llvm; using namespace clang; using namespace clangd; void clangd::foo(StringRef); This is in some of the older files. (It prevents accidentally defining a new function instead of one in the header file, for what that's worth). B) namespace clang { namespace clangd { void foo(llvm::StringRef) { ... } This is fine, but in practice the using directive often gets added over time. C) namespace clang { namespace clangd { using namespace llvm; // inside the namespace This was pretty common, but is a bit misleading: name lookup preferrs clang::clangd::foo > clang::foo > llvm:: foo (no matter where the using directive is). llvm-svn: 344850 --- clang-tools-extra/clangd/ClangdServer.cpp | 80 ++++++++++++++++--------------- 1 file changed, 41 insertions(+), 39 deletions(-) (limited to 'clang-tools-extra/clangd/ClangdServer.cpp') diff --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp index b0b1af1bbe4..cd1fb33efe6 100644 --- a/clang-tools-extra/clangd/ClangdServer.cpp +++ b/clang-tools-extra/clangd/ClangdServer.cpp @@ -34,13 +34,13 @@ #include #include -using namespace clang; -using namespace clang::clangd; - +using namespace llvm; +namespace clang { +namespace clangd { namespace { -void ignoreError(llvm::Error Err) { - handleAllErrors(std::move(Err), [](const llvm::ErrorInfoBase &) {}); +void ignoreError(Error Err) { + handleAllErrors(std::move(Err), [](const ErrorInfoBase &) {}); } std::string getStandardResourceDir() { @@ -51,7 +51,7 @@ std::string getStandardResourceDir() { class RefactoringResultCollector final : public tooling::RefactoringResultConsumer { public: - void handleError(llvm::Error Err) override { + void handleError(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 @@ -164,13 +164,13 @@ void ClangdServer::codeComplete(PathRef File, Position Pos, auto Task = [PCHs, Pos, FS, CodeCompleteOpts, this](Path File, Callback CB, - llvm::Expected IP) { + Expected IP) { if (!IP) return CB(IP.takeError()); if (isCancelled()) - return CB(llvm::make_error()); + return CB(make_error()); - llvm::Optional SpecFuzzyFind; + Optional SpecFuzzyFind; if (CodeCompleteOpts.Index && CodeCompleteOpts.SpeculativeIndexRequest) { SpecFuzzyFind.emplace(); { @@ -211,7 +211,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 CB, - llvm::Expected IP) { + Expected IP) { if (!IP) return CB(IP.takeError()); @@ -228,28 +228,28 @@ void ClangdServer::signatureHelp(PathRef File, Position Pos, Bind(Action, File.str(), std::move(CB))); } -llvm::Expected +Expected ClangdServer::formatRange(StringRef Code, PathRef File, Range Rng) { - llvm::Expected Begin = positionToOffset(Code, Rng.start); + Expected Begin = positionToOffset(Code, Rng.start); if (!Begin) return Begin.takeError(); - llvm::Expected End = positionToOffset(Code, Rng.end); + Expected End = positionToOffset(Code, Rng.end); if (!End) return End.takeError(); return formatCode(Code, File, {tooling::Range(*Begin, *End - *Begin)}); } -llvm::Expected ClangdServer::formatFile(StringRef Code, - PathRef File) { +Expected ClangdServer::formatFile(StringRef Code, + PathRef File) { // Format everything. return formatCode(Code, File, {tooling::Range(0, Code.size())}); } -llvm::Expected +Expected ClangdServer::formatOnType(StringRef Code, PathRef File, Position Pos) { // Look for the previous opening brace from the character position and // format starting from there. - llvm::Expected CursorPos = positionToOffset(Code, Pos); + Expected CursorPos = positionToOffset(Code, Pos); if (!CursorPos) return CursorPos.takeError(); size_t PreviousLBracePos = StringRef(Code).find_last_of('{', *CursorPos); @@ -260,7 +260,7 @@ ClangdServer::formatOnType(StringRef Code, PathRef File, Position Pos) { return formatCode(Code, File, {tooling::Range(PreviousLBracePos, Len)}); } -void ClangdServer::rename(PathRef File, Position Pos, llvm::StringRef NewName, +void ClangdServer::rename(PathRef File, Position Pos, StringRef NewName, Callback> CB) { auto Action = [Pos](Path File, std::string NewName, Callback> CB, @@ -312,16 +312,15 @@ void ClangdServer::rename(PathRef File, Position Pos, llvm::StringRef NewName, } void ClangdServer::dumpAST(PathRef File, - llvm::unique_function Callback) { - auto Action = [](decltype(Callback) Callback, - llvm::Expected InpAST) { + unique_function Callback) { + auto Action = [](decltype(Callback) Callback, Expected InpAST) { if (!InpAST) { ignoreError(InpAST.takeError()); return Callback(""); } std::string Result; - llvm::raw_string_ostream ResultOS(Result); + raw_string_ostream ResultOS(Result); clangd::dumpAST(InpAST->AST, ResultOS); ResultOS.flush(); @@ -334,7 +333,7 @@ void ClangdServer::dumpAST(PathRef File, void ClangdServer::findDefinitions(PathRef File, Position Pos, Callback> CB) { auto Action = [Pos, this](Callback> CB, - llvm::Expected InpAST) { + Expected InpAST) { if (!InpAST) return CB(InpAST.takeError()); CB(clangd::findDefinitions(InpAST->AST, Pos, Index)); @@ -343,13 +342,13 @@ void ClangdServer::findDefinitions(PathRef File, Position Pos, WorkScheduler.runWithAST("Definitions", File, Bind(Action, std::move(CB))); } -llvm::Optional ClangdServer::switchSourceHeader(PathRef Path) { +Optional ClangdServer::switchSourceHeader(PathRef Path) { StringRef SourceExtensions[] = {".cpp", ".c", ".cc", ".cxx", ".c++", ".m", ".mm"}; StringRef HeaderExtensions[] = {".h", ".hh", ".hpp", ".hxx", ".inc"}; - StringRef PathExt = llvm::sys::path::extension(Path); + StringRef PathExt = sys::path::extension(Path); // Lookup in a list of known extensions. auto SourceIter = @@ -367,7 +366,7 @@ llvm::Optional ClangdServer::switchSourceHeader(PathRef Path) { // We can only switch between the known extensions. if (!IsSource && !IsHeader) - return llvm::None; + return None; // Array to lookup extensions for the switch. An opposite of where original // extension was found. @@ -385,23 +384,23 @@ llvm::Optional ClangdServer::switchSourceHeader(PathRef Path) { // Loop through switched extension candidates. for (StringRef NewExt : NewExts) { - llvm::sys::path::replace_extension(NewPath, NewExt); + 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. - llvm::sys::path::replace_extension(NewPath, NewExt.upper()); + sys::path::replace_extension(NewPath, NewExt.upper()); if (FS->exists(NewPath)) return NewPath.str().str(); } - return llvm::None; + return None; } -llvm::Expected -ClangdServer::formatCode(llvm::StringRef Code, PathRef File, +Expected +ClangdServer::formatCode(StringRef Code, PathRef File, ArrayRef Ranges) { // Call clang-format. auto FS = FSProvider.getFileSystem(); @@ -425,7 +424,7 @@ ClangdServer::formatCode(llvm::StringRef Code, PathRef File, void ClangdServer::findDocumentHighlights( PathRef File, Position Pos, Callback> CB) { auto Action = [Pos](Callback> CB, - llvm::Expected InpAST) { + Expected InpAST) { if (!InpAST) return CB(InpAST.takeError()); CB(clangd::findDocumentHighlights(InpAST->AST, Pos)); @@ -435,9 +434,9 @@ void ClangdServer::findDocumentHighlights( } void ClangdServer::findHover(PathRef File, Position Pos, - Callback> CB) { - auto Action = [Pos](Callback> CB, - llvm::Expected InpAST) { + Callback> CB) { + auto Action = [Pos](Callback> CB, + Expected InpAST) { if (!InpAST) return CB(InpAST.takeError()); CB(clangd::getHover(InpAST->AST, Pos)); @@ -466,7 +465,7 @@ void ClangdServer::consumeDiagnostics(PathRef File, DocVersion Version, tooling::CompileCommand ClangdServer::getCompileCommand(PathRef File) { trace::Span Span("GetCompileCommand"); - llvm::Optional C = CDB.getCompileCommand(File); + Optional C = CDB.getCompileCommand(File); if (!C) // FIXME: Suppress diagnostics? Let the user know? C = CDB.getFallbackCommand(File); @@ -490,7 +489,7 @@ void ClangdServer::workspaceSymbols( void ClangdServer::documentSymbols( StringRef File, Callback> CB) { auto Action = [](Callback> CB, - llvm::Expected InpAST) { + Expected InpAST) { if (!InpAST) return CB(InpAST.takeError()); CB(clangd::getDocumentSymbols(InpAST->AST)); @@ -502,7 +501,7 @@ void ClangdServer::documentSymbols( void ClangdServer::findReferences(PathRef File, Position Pos, Callback> CB) { auto Action = [Pos, this](Callback> CB, - llvm::Expected InpAST) { + Expected InpAST) { if (!InpAST) return CB(InpAST.takeError()); CB(clangd::findReferences(InpAST->AST, Pos, Index)); @@ -517,6 +516,9 @@ ClangdServer::getUsedBytesPerFile() const { } LLVM_NODISCARD bool -ClangdServer::blockUntilIdleForTest(llvm::Optional TimeoutSeconds) { +ClangdServer::blockUntilIdleForTest(Optional TimeoutSeconds) { return WorkScheduler.blockUntilIdle(timeoutSeconds(TimeoutSeconds)); } + +} // namespace clangd +} // namespace clang -- cgit v1.2.3