Skip to content

Commit

Permalink
Merged master:687e1d712164 into amd-gfx:d4c310eb5e87
Browse files Browse the repository at this point in the history
Local branch amd-gfx d4c310e Merged master:15bff4dec436 into amd-gfx:2bd365f723db
Remote branch master 687e1d7 [clangd] makeStringError,make_error<StringError> -> error()
  • Loading branch information
Sw authored and Sw committed Sep 14, 2020
2 parents d4c310e + 687e1d7 commit 0be4f04
Show file tree
Hide file tree
Showing 58 changed files with 1,087 additions and 2,807 deletions.
28 changes: 9 additions & 19 deletions clang-tools-extra/clangd/ClangdLSPServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,9 @@ llvm::Error validateEdits(const DraftStore &DraftMgr, const FileEdits &FE) {
if (!InvalidFileCount)
return llvm::Error::success();
if (InvalidFileCount == 1)
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"File must be saved first: " +
LastInvalidFile);
return llvm::createStringError(
llvm::inconvertibleErrorCode(),
"Files must be saved first: " + LastInvalidFile + " (and " +
llvm::to_string(InvalidFileCount - 1) + " others)");
return error("File must be saved first: {0}", LastInvalidFile);
return error("Files must be saved first: {0} (and {1} others)",
LastInvalidFile, InvalidFileCount - 1);
}

} // namespace
Expand Down Expand Up @@ -284,10 +280,9 @@ class ClangdLSPServer::MessageHandler : public Transport::MessageHandler {
}
}
if (OldestCB)
OldestCB->second(llvm::createStringError(
llvm::inconvertibleErrorCode(),
llvm::formatv("failed to receive a client reply for request ({0})",
OldestCB->first)));
OldestCB->second(
error("failed to receive a client reply for request ({0})",
OldestCB->first));
return ID;
}

Expand Down Expand Up @@ -661,8 +656,7 @@ void ClangdLSPServer::onSync(const NoParams &Params,
if (Server->blockUntilIdleForTest(/*TimeoutSeconds=*/60))
Reply(nullptr);
else
Reply(llvm::createStringError(llvm::inconvertibleErrorCode(),
"Not idle after a minute"));
Reply(error("Not idle after a minute"));
}

void ClangdLSPServer::onDocumentDidOpen(
Expand Down Expand Up @@ -729,9 +723,7 @@ void ClangdLSPServer::onCommand(const ExecuteCommandParams &Params,
std::string Reason = Response->failureReason
? *Response->failureReason
: "unknown reason";
return Reply(llvm::createStringError(
llvm::inconvertibleErrorCode(),
("edits were not applied: " + Reason).c_str()));
return Reply(error("edits were not applied: {0}", Reason));
}
return Reply(SuccessMessage);
});
Expand All @@ -752,9 +744,7 @@ void ClangdLSPServer::onCommand(const ExecuteCommandParams &Params,
Params.tweakArgs) {
auto Code = DraftMgr.getDraft(Params.tweakArgs->file.file());
if (!Code)
return Reply(llvm::createStringError(
llvm::inconvertibleErrorCode(),
"trying to apply a code action for a non-added file"));
return Reply(error("trying to apply a code action for a non-added file"));

auto Action = [this, ApplyEdit, Reply = std::move(Reply),
File = Params.tweakArgs->file, Code = std::move(*Code)](
Expand Down
10 changes: 7 additions & 3 deletions clang-tools-extra/clangd/ClangdServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,7 @@ void ClangdServer::signatureHelp(PathRef File, Position Pos,

const auto *PreambleData = IP->Preamble;
if (!PreambleData)
return CB(llvm::createStringError(llvm::inconvertibleErrorCode(),
"Failed to parse includes"));
return CB(error("Failed to parse includes"));

ParseInputs ParseInput{IP->Command, &TFS, IP->Contents.str()};
ParseInput.Index = Index;
Expand Down Expand Up @@ -537,9 +536,12 @@ void ClangdServer::enumerateTweaks(PathRef File, Range Sel,

void ClangdServer::applyTweak(PathRef File, Range Sel, StringRef TweakID,
Callback<Tweak::Effect> CB) {
// Tracks number of times a tweak has been applied.
// Tracks number of times a tweak has been attempted.
static constexpr trace::Metric TweakAttempt(
"tweak_attempt", trace::Metric::Counter, "tweak_id");
// Tracks number of times a tweak has failed to produce edits.
static constexpr trace::Metric TweakFailed(
"tweak_failed", trace::Metric::Counter, "tweak_id");
TweakAttempt.record(1, TweakID);
auto Action = [File = File.str(), Sel, TweakID = TweakID.str(),
CB = std::move(CB),
Expand Down Expand Up @@ -570,6 +572,8 @@ void ClangdServer::applyTweak(PathRef File, Range Sel, StringRef TweakID,
if (llvm::Error Err = reformatEdit(E, Style))
elog("Failed to format {0}: {1}", It.first(), std::move(Err));
}
} else {
TweakFailed.record(1, TweakID);
}
return CB(std::move(*Effect));
};
Expand Down
3 changes: 1 addition & 2 deletions clang-tools-extra/clangd/CodeComplete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,7 @@ struct CodeCompletionBuilder {
return ResolvedInserted.takeError();
auto Spelled = Includes.calculateIncludePath(*ResolvedInserted, FileName);
if (!Spelled)
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"Header not on include path");
return error("Header not on include path");
return std::make_pair(
std::move(*Spelled),
Includes.shouldInsertInclude(*ResolvedDeclaring, *ResolvedInserted));
Expand Down
23 changes: 10 additions & 13 deletions clang-tools-extra/clangd/DraftStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ llvm::Expected<DraftStore::Draft> DraftStore::updateDraft(

auto EntryIt = Drafts.find(File);
if (EntryIt == Drafts.end()) {
return llvm::make_error<llvm::StringError>(
"Trying to do incremental update on non-added document: " + File,
llvm::errc::invalid_argument);
return error(llvm::errc::invalid_argument,
"Trying to do incremental update on non-added document: {0}",
File);
}
Draft &D = EntryIt->second;
std::string Contents = EntryIt->second.Contents;
Expand All @@ -89,11 +89,9 @@ llvm::Expected<DraftStore::Draft> DraftStore::updateDraft(
return EndIndex.takeError();

if (*EndIndex < *StartIndex)
return llvm::make_error<llvm::StringError>(
llvm::formatv(
"Range's end position ({0}) is before start position ({1})", End,
Start),
llvm::errc::invalid_argument);
return error(llvm::errc::invalid_argument,
"Range's end position ({0}) is before start position ({1})",
End, Start);

// Since the range length between two LSP positions is dependent on the
// contents of the buffer we compute the range length between the start and
Expand All @@ -106,11 +104,10 @@ llvm::Expected<DraftStore::Draft> DraftStore::updateDraft(
lspLength(Contents.substr(*StartIndex, *EndIndex - *StartIndex));

if (Change.rangeLength && ComputedRangeLength != *Change.rangeLength)
return llvm::make_error<llvm::StringError>(
llvm::formatv("Change's rangeLength ({0}) doesn't match the "
"computed range length ({1}).",
*Change.rangeLength, ComputedRangeLength),
llvm::errc::invalid_argument);
return error(llvm::errc::invalid_argument,
"Change's rangeLength ({0}) doesn't match the "
"computed range length ({1}).",
*Change.rangeLength, ComputedRangeLength);

std::string NewContents;
NewContents.reserve(*StartIndex + Change.text.length() +
Expand Down
9 changes: 3 additions & 6 deletions clang-tools-extra/clangd/FindSymbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,9 @@ struct ScoredSymbolGreater {
llvm::Expected<Location> indexToLSPLocation(const SymbolLocation &Loc,
llvm::StringRef TUPath) {
auto Path = URI::resolve(Loc.FileURI, TUPath);
if (!Path) {
return llvm::make_error<llvm::StringError>(
llvm::formatv("Could not resolve path for file '{0}': {1}", Loc.FileURI,
llvm::toString(Path.takeError())),
llvm::inconvertibleErrorCode());
}
if (!Path)
return error("Could not resolve path for file '{0}': {1}", Loc.FileURI,
Path.takeError());
Location L;
L.uri = URIForFile::canonicalize(*Path, TUPath);
Position Start, End;
Expand Down
3 changes: 1 addition & 2 deletions clang-tools-extra/clangd/IncludeFixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ std::vector<Fix> IncludeFixer::fixesForSymbols(const SymbolSlab &Syms) const {
return ResolvedInserted.takeError();
auto Spelled = Inserter->calculateIncludePath(*ResolvedInserted, File);
if (!Spelled)
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"Header not on include path");
return error("Header not on include path");
return std::make_pair(
std::move(*Spelled),
Inserter->shouldInsertInclude(*ResolvedDeclaring, *ResolvedInserted));
Expand Down
14 changes: 6 additions & 8 deletions clang-tools-extra/clangd/JSONTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "support/Shutdown.h"
#include "llvm/Support/Errno.h"
#include "llvm/Support/Error.h"
#include <system_error>

namespace clang {
namespace clangd {
Expand Down Expand Up @@ -51,12 +52,10 @@ llvm::json::Object encodeError(llvm::Error E) {
}

llvm::Error decodeError(const llvm::json::Object &O) {
std::string Msg =
std::string(O.getString("message").getValueOr("Unspecified error"));
llvm::StringRef Msg = O.getString("message").getValueOr("Unspecified error");
if (auto Code = O.getInteger("code"))
return llvm::make_error<LSPError>(std::move(Msg), ErrorCode(*Code));
return llvm::make_error<llvm::StringError>(std::move(Msg),
llvm::inconvertibleErrorCode());
return llvm::make_error<LSPError>(Msg.str(), ErrorCode(*Code));
return error(Msg.str());
}

class JSONTransport : public Transport {
Expand Down Expand Up @@ -102,9 +101,8 @@ class JSONTransport : public Transport {
llvm::Error loop(MessageHandler &Handler) override {
while (!feof(In)) {
if (shutdownRequested())
return llvm::createStringError(
std::make_error_code(std::errc::operation_canceled),
"Got signal, shutting down");
return error(std::make_error_code(std::errc::operation_canceled),
"Got signal, shutting down");
if (ferror(In))
return llvm::errorCodeToError(
std::error_code(errno, std::system_category()));
Expand Down
8 changes: 3 additions & 5 deletions clang-tools-extra/clangd/PathMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "PathMapping.h"
#include "Transport.h"
#include "URI.h"
#include "support/Logger.h"
#include "llvm/ADT/None.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Errno.h"
Expand Down Expand Up @@ -156,8 +157,7 @@ llvm::Expected<std::string> parsePath(llvm::StringRef Path) {
Converted = "/" + Converted;
return Converted;
}
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"Path not absolute: " + Path);
return error("Path not absolute: {0}", Path);
}

} // namespace
Expand All @@ -174,9 +174,7 @@ parsePathMappings(llvm::StringRef RawPathMappings) {
std::tie(PathPair, Rest) = Rest.split(",");
std::tie(ClientPath, ServerPath) = PathPair.split("=");
if (ClientPath.empty() || ServerPath.empty())
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"Not a valid path mapping pair: " +
PathPair);
return error("Not a valid path mapping pair: {0}", PathPair);
llvm::Expected<std::string> ParsedClientPath = parsePath(ClientPath);
if (!ParsedClientPath)
return ParsedClientPath.takeError();
Expand Down
9 changes: 3 additions & 6 deletions clang-tools-extra/clangd/Preamble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,7 @@ scanPreamble(llvm::StringRef Contents, const tooling::CompileCommand &Cmd) {
IgnoringDiagConsumer IgnoreDiags;
auto CI = buildCompilerInvocation(PI, IgnoreDiags);
if (!CI)
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"failed to create compiler invocation");
return error("failed to create compiler invocation");
CI->getDiagnosticOpts().IgnoreWarnings = true;
auto ContentsBuffer = llvm::MemoryBuffer::getMemBuffer(Contents);
// This means we're scanning (though not preprocessing) the preamble section
Expand All @@ -260,14 +259,12 @@ scanPreamble(llvm::StringRef Contents, const tooling::CompileCommand &Cmd) {
// also implies missing resolved paths for includes.
FS.view(llvm::None), IgnoreDiags);
if (Clang->getFrontendOpts().Inputs.empty())
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"compiler instance had no inputs");
return error("compiler instance had no inputs");
// We are only interested in main file includes.
Clang->getPreprocessorOpts().SingleFileParseMode = true;
PreprocessOnlyAction Action;
if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0]))
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"failed BeginSourceFile");
return error("failed BeginSourceFile");
const auto &SM = Clang->getSourceManager();
Preprocessor &PP = Clang->getPreprocessor();
IncludeStructure Includes;
Expand Down
19 changes: 6 additions & 13 deletions clang-tools-extra/clangd/RIFF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,28 @@
//===----------------------------------------------------------------------===//

#include "RIFF.h"
#include "support/Logger.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/Error.h"

namespace clang {
namespace clangd {
namespace riff {

static llvm::Error makeError(const llvm::Twine &Msg) {
return llvm::make_error<llvm::StringError>(Msg,
llvm::inconvertibleErrorCode());
}

llvm::Expected<Chunk> readChunk(llvm::StringRef &Stream) {
if (Stream.size() < 8)
return makeError("incomplete chunk header: " + llvm::Twine(Stream.size()) +
" bytes available");
return error("incomplete chunk header: {0} bytes available", Stream.size());
Chunk C;
std::copy(Stream.begin(), Stream.begin() + 4, C.ID.begin());
Stream = Stream.drop_front(4);
uint32_t Len = llvm::support::endian::read32le(Stream.take_front(4).begin());
Stream = Stream.drop_front(4);
if (Stream.size() < Len)
return makeError("truncated chunk: want " + llvm::Twine(Len) + ", got " +
llvm::Twine(Stream.size()));
return error("truncated chunk: want {0}, got {1}", Len, Stream.size());
C.Data = Stream.take_front(Len);
Stream = Stream.drop_front(Len);
if ((Len % 2) && !Stream.empty()) { // Skip padding byte.
if (Stream.front())
return makeError("nonzero padding byte");
return error("nonzero padding byte");
Stream = Stream.drop_front();
}
return std::move(C);
Expand All @@ -57,9 +50,9 @@ llvm::Expected<File> readFile(llvm::StringRef Stream) {
if (!RIFF)
return RIFF.takeError();
if (RIFF->ID != fourCC("RIFF"))
return makeError("not a RIFF container: root is " + fourCCStr(RIFF->ID));
return error("not a RIFF container: root is {0}", fourCCStr(RIFF->ID));
if (RIFF->Data.size() < 4)
return makeError("RIFF chunk too short");
return error("RIFF chunk too short");
File F;
std::copy(RIFF->Data.begin(), RIFF->Data.begin() + 4, F.Type.begin());
for (llvm::StringRef Body = RIFF->Data.drop_front(4); !Body.empty();)
Expand Down
22 changes: 9 additions & 13 deletions clang-tools-extra/clangd/SourceCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,20 +175,17 @@ size_t lspLength(llvm::StringRef Code) {
llvm::Expected<size_t> positionToOffset(llvm::StringRef Code, Position P,
bool AllowColumnsBeyondLineLength) {
if (P.line < 0)
return llvm::make_error<llvm::StringError>(
llvm::formatv("Line value can't be negative ({0})", P.line),
llvm::errc::invalid_argument);
return error(llvm::errc::invalid_argument,
"Line value can't be negative ({0})", P.line);
if (P.character < 0)
return llvm::make_error<llvm::StringError>(
llvm::formatv("Character value can't be negative ({0})", P.character),
llvm::errc::invalid_argument);
return error(llvm::errc::invalid_argument,
"Character value can't be negative ({0})", P.character);
size_t StartOfLine = 0;
for (int I = 0; I != P.line; ++I) {
size_t NextNL = Code.find('\n', StartOfLine);
if (NextNL == llvm::StringRef::npos)
return llvm::make_error<llvm::StringError>(
llvm::formatv("Line value is out of range ({0})", P.line),
llvm::errc::invalid_argument);
return error(llvm::errc::invalid_argument,
"Line value is out of range ({0})", P.line);
StartOfLine = NextNL + 1;
}
StringRef Line =
Expand All @@ -198,10 +195,9 @@ llvm::Expected<size_t> positionToOffset(llvm::StringRef Code, Position P,
bool Valid;
size_t ByteInLine = measureUnits(Line, P.character, lspEncoding(), Valid);
if (!Valid && !AllowColumnsBeyondLineLength)
return llvm::make_error<llvm::StringError>(
llvm::formatv("{0} offset {1} is invalid for line {2}", lspEncoding(),
P.character, P.line),
llvm::errc::invalid_argument);
return error(llvm::errc::invalid_argument,
"{0} offset {1} is invalid for line {2}", lspEncoding(),
P.character, P.line);
return StartOfLine + ByteInLine;
}

Expand Down
3 changes: 1 addition & 2 deletions clang-tools-extra/clangd/TUScheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -717,8 +717,7 @@ void ASTWorker::runWithAST(
[&AST, this]() { IdleASTs.put(this, std::move(*AST)); });
// Run the user-provided action.
if (!*AST)
return Action(llvm::make_error<llvm::StringError>(
"invalid AST", llvm::errc::invalid_argument));
return Action(error(llvm::errc::invalid_argument, "invalid AST"));
vlog("ASTWorker running {0} on version {2} of {1}", Name, FileName,
FileInputs.Version);
Action(InputsAndAST{FileInputs, **AST});
Expand Down
Loading

0 comments on commit 0be4f04

Please sign in to comment.