Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@ For more information on Checked C and pointers to example code, see our

## 3C: Semi-automated conversion of C code to Checked C

This repository includes a tool called 3C that partially automates the
conversion of C code to Checked C. Here is [general information about the 3C
software](clang/docs/checkedc/3C/README.md), including development status and
how to contribute. Here are the [usage instructions for the `3c` command-line
tool](clang/tools/3c/README.md).
This repository includes a tool called 3C that partially automates the conversion of C code to Checked C. Here is [general information about the 3C software](clang/docs/checkedc/3C/README.md), including development status and how to contribute. Here are the [usage instructions for the `3c` command-line tool](clang/tools/3c/README.md).

## More information

Expand Down
11 changes: 6 additions & 5 deletions clang-tools-extra/clangd/3CCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ void As3CCommands(const Diagnostic &D, std::vector<Command> &OutCommands) {
}

bool Is3CCommand(const ExecuteCommandParams &Params) {
return (Params.command.rfind(Command::_3C_APPLY_ONLY_FOR_THIS, 0) == 0) ||
(Params.command.rfind(Command::_3C_APPLY_FOR_ALL, 0) == 0);
return (Params.command.rfind(Command::_3C_APPLY_ONLY_FOR_THIS, 0) == 0) ||
(Params.command.rfind(Command::_3C_APPLY_FOR_ALL, 0) == 0);
}

bool Execute3CCommand(const ExecuteCommandParams &Params,
std::string &ReplyMessage, _3CInterface &CcInterface) {
std::string &ReplyMessage,
_3CInterface &CcInterface) {
ReplyMessage = "Checked C Pointer Modified.";
if (Params.command.rfind(Command::_3C_APPLY_ONLY_FOR_THIS, 0) == 0) {
int PtrId = Params._3CManualFix->ptrID;
Expand All @@ -67,6 +68,6 @@ bool Execute3CCommand(const ExecuteCommandParams &Params,
}
return false;
}
} // namespace clangd
} // namespace clang
}
}
#endif
23 changes: 12 additions & 11 deletions clang-tools-extra/clangd/3CCommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@

namespace clang {
namespace clangd {
// Convert the provided Diagnostic into Commands.
void As3CCommands(const Diagnostic &D, std::vector<Command> &OutCommands);
// Check if the execute command request from the client is a 3C command.
bool Is3CCommand(const ExecuteCommandParams &Params);
// Convert the provided Diagnostic into Commands
void As3CCommands(const Diagnostic &D, std::vector<Command> &OutCommands);
// Check if the execute command request from the client is a 3C command.
bool Is3CCommand(const ExecuteCommandParams &Params);

// Interpret the provided execute command request as 3C command
// and execute them.
bool Execute3CCommand(const ExecuteCommandParams &Params,
std::string &ReplyMessage, _3CInterface &CcInterface);
} // namespace clangd
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_3CCOMMANDS_H
// Interpret the provided execute command request as 3C command
// and execute them.
bool Execute3CCommand(const ExecuteCommandParams &Params,
std::string &ReplyMessage,
_3CInterface &CcInterface);
}
}
#endif //LLVM_CLANG_TOOLS_EXTRA_CLANGD_3CCOMMANDS_H
#endif
21 changes: 11 additions & 10 deletions clang-tools-extra/clangd/3CDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ static bool IsValidSourceFile(ConstraintsInfo &CCRes, std::string &FilePath) {
return CCRes.ValidSourceFiles.find(FilePath) != CCRes.ValidSourceFiles.end();
}


bool _3CDiagnostics::PopulateDiagsFromConstraintsInfo(ConstraintsInfo &Line) {
std::lock_guard<std::mutex> Lock(DiagMutex);
std::set<ConstraintKey> ProcessedCKeys;
ProcessedCKeys.clear();
auto GetLocRange = [](uint32_t Line, uint32_t ColNoS,
uint32_t ColNoE) -> Range {
auto GetLocRange = [](uint32_t Line, uint32_t ColNoS, uint32_t ColNoE) -> Range {
Range nRange;
Line--;
nRange.start.line = Line;
Expand Down Expand Up @@ -60,8 +60,8 @@ bool _3CDiagnostics::PopulateDiagsFromConstraintsInfo(ConstraintsInfo &Line) {
NewDiag.Source = Diag::_3CMain;
NewDiag.Severity = DiagnosticsEngine::Level::Error;
NewDiag.code = std::to_string(WReason.first);
NewDiag.Message =
"Pointer is wild because of:" + WReason.second.getWildPtrReason();
NewDiag.Message = "Pointer is wild because of:" +
WReason.second.getWildPtrReason();

// Create notes for the information about root cause.
PersistentSourceLoc SL = WReason.second.getLocation();
Expand Down Expand Up @@ -110,11 +110,11 @@ bool _3CDiagnostics::PopulateDiagsFromConstraintsInfo(ConstraintsInfo &Line) {
PsInfo = Line.AtomSourceMap[tC];
FilePath = PsInfo->getFileName();
DiagNote.AbsFile = FilePath;
DiagNote.Range = GetLocRange(
PsInfo->getLineNo(), PsInfo->getColSNo(), PsInfo->getColENo());
DiagNote.Range =
GetLocRange(PsInfo->getLineNo(), PsInfo->getColSNo(),
PsInfo->getColENo());
MaxPtrReasons--;
DiagNote.Message =
Line.RootWildAtomsWithReason[tC].getWildPtrReason();
DiagNote.Message = Line.RootWildAtomsWithReason[tC].getWildPtrReason();
if (MaxPtrReasons <= 1)
DiagNote.Message += " (others)";
NewDiag.Notes.push_back(DiagNote);
Expand All @@ -127,9 +127,10 @@ bool _3CDiagnostics::PopulateDiagsFromConstraintsInfo(ConstraintsInfo &Line) {
}
}


return true;
}

} // namespace clangd
} // namespace clang
}
}
#endif
11 changes: 6 additions & 5 deletions clang-tools-extra/clangd/3CDiagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_3CDIAGNOSTICS_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_3CDIAGNOSTICS_H

#include <set>
#include "Diagnostics.h"
#include "clang/3C/3C.h"
#include <set>

namespace clang {
namespace clangd {
Expand All @@ -33,12 +33,13 @@ class _3CDiagnostics {
std::map<std::string, std::vector<Diag>> &GetAllFilesDiagnostics() {
return AllFileDiagnostics;
}

private:
// Diagnostics of all files.
std::map<std::string, std::vector<Diag>> AllFileDiagnostics;


};
} // namespace clangd
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_
}
}
#endif //LLVM_CLANG_TOOLS_EXTRA_CLANGD_
#endif
32 changes: 19 additions & 13 deletions clang-tools-extra/clangd/ClangdLSPServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,8 @@ void ClangdLSPServer::onInitialize(const InitializeParams &Params,
llvm::json::Object{
{"textDocumentSync", (int)TextDocumentSyncKind::Incremental},
{"codeActionProvider", true},
{"codeLensProvider", llvm::json::Object{{"resolveProvider", true}}},
{"codeLensProvider", llvm::json::Object{
{"resolveProvider", true}}},
{"executeCommandProvider",
llvm::json::Object{
{"commands",
Expand Down Expand Up @@ -528,35 +529,40 @@ void ClangdLSPServer::onFileEvent(const DidChangeWatchedFilesParams &Params) {
}

#ifdef INTERACTIVE3C
void ClangdLSPServer::_3CResultsReady(std::string FileName, bool ClearDiags) {
void ClangdLSPServer::_3CResultsReady(std::string FileName,
bool ClearDiags) {
// Get the diagnostics and update the client.
std::vector<Diag> Diagnostics;
Diagnostics.clear();
if (!ClearDiags) {
std::lock_guard<std::mutex> lock(Server->_3CDiagInfo.DiagMutex);
auto &allDiags = Server->_3CDiagInfo.GetAllFilesDiagnostics();
if (allDiags.find(FileName) != allDiags.end()) {
Diagnostics.insert(Diagnostics.begin(), allDiags[FileName].begin(),
allDiags[FileName].end());
if (allDiags.find(FileName) !=
allDiags.end()) {
Diagnostics.insert(
Diagnostics.begin(),
allDiags[FileName].begin(),
allDiags[FileName].end());
}
}
this->onDiagnosticsReady(FileName, Diagnostics);
}

void ClangdLSPServer::send3CMessage(std::string MsgStr) {
// Send message as info to the client.
notify("window/showMessage", llvm::json::Object{
// Info message.
{"type", 3},
{"message", std::move(MsgStr)},
});
// Send message as info to the client.
notify("window/showMessage",
llvm::json::Object{
// Info message.
{"type", 3},
{"message", std::move(MsgStr)},
});
}
#endif
void ClangdLSPServer::onCommand(const ExecuteCommandParams &Params,
Callback<llvm::json::Value> Reply) {
#ifdef INTERACTIVE3C
// In this mode, we support only 3C commands.
if (Is3CCommand(Params)) {
if(Is3CCommand(Params)) {
Server->execute3CCommand(Params, this);
Reply("3C Background work scheduled.");
} else {
Expand Down Expand Up @@ -1143,6 +1149,7 @@ ClangdLSPServer::ClangdLSPServer(
MsgHandler->bind("textDocument/didOpen", &ClangdLSPServer::onDocumentDidOpen);
MsgHandler->bind("textDocument/didClose", &ClangdLSPServer::onDocumentDidClose);
MsgHandler->bind("textDocument/didChange", &ClangdLSPServer::onDocumentDidChange);
// clang-format on
#else
MsgHandler->bind("initialize", &ClangdLSPServer::onInitialize);
MsgHandler->bind("shutdown", &ClangdLSPServer::onShutdown);
Expand Down Expand Up @@ -1172,7 +1179,6 @@ ClangdLSPServer::ClangdLSPServer(
MsgHandler->bind("textDocument/typeHierarchy", &ClangdLSPServer::onTypeHierarchy);
MsgHandler->bind("typeHierarchy/resolve", &ClangdLSPServer::onResolveTypeHierarchy);
#endif
// clang-format on
}

ClangdLSPServer::~ClangdLSPServer() = default;
Expand Down
17 changes: 8 additions & 9 deletions clang-tools-extra/clangd/ClangdServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
#include "refactor/Rename.h"
#include "refactor/Tweak.h"
#ifdef INTERACTIVE3C
#include "3CCommands.h"
#include "clang/3C/3C.h"
#include "3CCommands.h"
#endif
#include "clang/Format/Format.h"
#include "clang/Frontend/CompilerInstance.h"
Expand Down Expand Up @@ -122,10 +122,9 @@ ClangdServer::ClangdServer(const GlobalCompilationDatabase &CDB,
DynamicIdx.get(), DiagConsumer, Opts.SemanticHighlighting),
Opts.UpdateDebounce, Opts.RetentionPolicy)
#ifdef INTERACTIVE3C
,
_3CInter(_3CInterface)
, _3CInter(_3CInterface)
#endif
{
{
// Adds an index to the stack, at higher priority than existing indexes.
auto AddIndex = [&](SymbolIndex *Idx) {
if (this->Index != nullptr) {
Expand Down Expand Up @@ -187,8 +186,8 @@ void ClangdServer::clear3CDiagsForAllFiles(ConstraintsInfo &CcInfo,
}
}

void ClangdServer::_3CCollectAndBuildInitialConstraints(
_3CLSPCallBack *ConvCB) {
void
ClangdServer::_3CCollectAndBuildInitialConstraints(_3CLSPCallBack *ConvCB) {
auto Task = [=]() {
_3CDiagInfo.ClearAllDiags();
ConvCB->send3CMessage("Running 3C for first time.");
Expand All @@ -213,7 +212,8 @@ void ClangdServer::execute3CCommand(ExecuteCommandParams Params,
std::string RplMsg;
auto &WildPtrsInfo = _3CInter.GetWILDPtrsInfo();
auto &PtrSourceMap = WildPtrsInfo.AtomSourceMap;
if (PtrSourceMap.find(Params._3CManualFix->ptrID) != PtrSourceMap.end()) {
if (PtrSourceMap.find(Params._3CManualFix->ptrID) !=
PtrSourceMap.end()) {
std::string PtrFileName =
PtrSourceMap[Params._3CManualFix->ptrID]->getFileName();
log("3C: File of the pointer {0}\n", PtrFileName);
Expand Down Expand Up @@ -242,8 +242,7 @@ void ClangdServer::_3CCloseDocument(std::string FileName) {
log("3C: Finished writing back file: {0}\n", FileName);
} else {
log("3C: File not included during constraint solving phase. "
"Rewriting failed: {0}\n",
FileName);
"Rewriting failed: {0}\n", FileName);
}
};
WorkScheduler.run("3C: Writing back file.", Task);
Expand Down
6 changes: 3 additions & 3 deletions clang-tools-extra/clangd/ClangdServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ class ClangdServer {
// collect and build initial set of constraints on the source
// files.

void execute3CCommand(ExecuteCommandParams Params, _3CLSPCallBack *ConvCB);
void execute3CCommand(ExecuteCommandParams Params,
_3CLSPCallBack *ConvCB);

void _3CCollectAndBuildInitialConstraints(_3CLSPCallBack *ConvCB);

Expand All @@ -327,8 +328,7 @@ class ClangdServer {
ArrayRef<tooling::Range> Ranges);

#ifdef INTERACTIVE3C
void report3CDiagsForAllFiles(ConstraintsInfo &CcInfo,
_3CLSPCallBack *ConvCB);
void report3CDiagsForAllFiles(ConstraintsInfo &CcInfo, _3CLSPCallBack *ConvCB);
void clear3CDiagsForAllFiles(ConstraintsInfo &CcInfo, _3CLSPCallBack *ConvCB);
#endif

Expand Down
3 changes: 2 additions & 1 deletion clang-tools-extra/clangd/Protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,8 @@ bool fromJSON(const llvm::json::Value &Params, ExecuteCommandParams &R) {
#ifdef INTERACTIVE3C
if (R.command == ExecuteCommandParams::_3C_APPLY_ONLY_FOR_THIS ||
R.command == ExecuteCommandParams::_3C_APPLY_FOR_ALL) {
return Args && Args->size() == 1 && fromJSON(Args->front(), R._3CManualFix);
return Args && Args->size() == 1 &&
fromJSON(Args->front(), R._3CManualFix);
}
#endif
return false; // Unrecognized command.
Expand Down
12 changes: 5 additions & 7 deletions clang-tools-extra/clangd/Protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -817,25 +817,23 @@ llvm::json::Value toJSON(const CodeAction &);
// A code lens represents a command that should be shown along with
// source text, like the number of references, a way to run tests, etc.
//
// A code lens is _unresolved_ when no command is associated to it. For
// performance reasons the creation of a code lens and resolving should be done
// in two stages.
// A code lens is _unresolved_ when no command is associated to it. For performance
// reasons the creation of a code lens and resolving should be done in two stages.
//
struct CodeLens {
/// The range in which this code lens is valid, should only span a single
/// line.
/// The range in which this code lens is valid, should only span a single line.
Range range;

/**
* The command this code lens represents.
*/
*/
llvm::Optional<Command> command;

/**
* A data entry field that is preserved on a code lens item between
* a code lens and a code lens resolve request.
*/
// data?: any
//data?: any
};

llvm::json::Value toJSON(const CodeLens &);
Expand Down
Loading