Files
revng-revng/lib/ThreadSafeClangTooling/ThreadSafeClangTooling.cpp
T
Pietro Fezzardi b092c4f505 Add ThreadSafeClangTooling library
This library provides a thin locking wrapper around clang::tooling
invocations.
It should be used instead of performing direct clang::tooling
invocations by all programs that use revng-c and may run more than one
ClangTool concurrently.

This is necessary because clang::tooling internally uses llvm's cl::opt
for parsing command line options.
cl::opt uses a global variable for the parser under the hood so parsing
two command lines concurrently is not safe.
Similarly, cl::opt typically uses global variables to hold options, so
it is not safe to execute a ClangTool concurrently to another tool
that is parsing a new set of options, because there might be race
conditions between threads reading and writing the same options at the
same time.

The new library introduces a thin locking layer so that the end-user
does not need to know or worry about these details.
2021-03-05 16:12:41 +01:00

40 lines
1.3 KiB
C++

//
// Copyright rev.ng Srls. See LICENSE.md for details.
//
#include <utility>
#include "clang/Tooling/Tooling.h"
#include "revng-c/ThreadSafeClangTooling/ThreadSafeClangTooling.h"
namespace revng {
namespace c {
const std::vector<std::string> ClangToolDefaultArgs{ // C language
"-xc",
// C11 dialect
"-std=c11"
};
} // namespace c
} // namespace revng
std::mutex ClangToolingMutex;
bool runThreadSafeClangTool(std::unique_ptr<clang::FrontendAction> ToolAction,
const std::string &Code,
const std::vector<std::string> &Args) {
std::scoped_lock ClangToolingGuard{ ClangToolingMutex };
return clang::tooling::runToolOnCodeWithArgs(std::move(ToolAction),
Code,
Args);
}
bool runThreadSafeClangTool(std::unique_ptr<clang::FrontendAction> ToolAction,
const std::string &Code) {
return runThreadSafeClangTool(std::move(ToolAction),
Code,
revng::c::ClangToolDefaultArgs);
}