Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Eval #11

Merged
merged 8 commits into from
Dec 1, 2024
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: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
cxx: "cl",
boost_toolset: msvc,
cmake_generator: "Visual Studio 17 2022",
llvm_tag: "llvmorg-18.1.8",
llvm_tag: "llvmorg-19.1.4",
python_version: "3.10",
os_version: "2022",
artifact_id: "RG3_Windows"
Expand All @@ -35,7 +35,7 @@ jobs:
cxx: "g++-13",
boost_toolset: gcc,
cmake_generator: "Ninja",
llvm_tag: "llvmorg-18.1.8",
llvm_tag: "llvmorg-19.1.4",
python_version: "3.10",
os_version: "24.04",
artifact_id: "RG3_Linux"
Expand All @@ -48,7 +48,7 @@ jobs:
cxx: "clang++",
boost_toolset: gcc,
cmake_generator: "Ninja",
llvm_tag: "llvmorg-18.1.8",
llvm_tag: "llvmorg-19.1.4",
python_version: "3.10",
os_version: "13",
artifact_id: "RG3_macOS"
Expand Down
47 changes: 42 additions & 5 deletions Extension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Install
Make sure that your system has clang (any version):
* **macOS**: you need to install XCode (tested on 15.x but should work everywhere)
* **Window**: you need to install clang 17.x or later and add it into PATH
* **Linux**: gcc & g++ at least 13 version (temporary limitation, planned to fix at 0.0.4)
* **Linux**: gcc & g++ at least 13 version
* **Other platforms & archs**: Contact us in [our GitHub](https://github.com/DronCode/RG3).

It's a better way to use RG3 inside virtualenv:
Expand Down Expand Up @@ -99,16 +99,53 @@ We have a type my::cool::name_space::SomeThirdPartyStruct (TK_STRUCT_OR_CLASS)
Function static bool IsGeniusDesc(bool bCanReplace)
```

Another good use case is `constexpr` evaluation feature:
```python
import rg3py
from typing import Union, List, Dict

evaluator: rg3py.CodeEvaluator = rg3py.CodeEvaluator.make_from_system_env()

evaluator.set_compiler_config({
"cpp_standard" : rg3py.CppStandard.CXX_20
})

result: Union[Dict[str, any], List[rg3py.CppCompilerIssue]] = evaluator.eval("""
#include <type_traits>

class Simple {};
class Base {};
class Inherited : public Base {};

constexpr bool r0 = std::is_base_of_v<Base, Inherited>;
constexpr bool r1 = std::is_base_of_v<Base, Simple>;
""", ["r0", "r1"])

print(result)
```

output will be

```text
{'r1': False, 'r0': True}
```

and that's great feature to make some checks like `type should be inherited from A, have some methods and etc...`

And this is independent of your current environment, only C++ STL library should be found!


Features
---------

* Supported Windows (x86_64), Linux (x86_64) and macOS (x86_64 and ARM64)
* Supported C++03, 11, 14, 17, 20, 23 (26 in theory, need to migrate to next LLVM)
* Supported C++03, 11, 14, 17, 20, 23, 26
* Supported threads in analysis on native side (see Tests/PyIntegration/test.py test: **test_analyzer_context_sample** for example)
* Statically linked, no external dependencies (except Clang instance on machine)
* Special macro definitions to hide unnecessary code
* Template specializations reporting
* Anonymous registration without changes in third party code
* Runtime constexpr C++ code evaluation with results extraction

Current limitations
-------------------
Expand All @@ -118,8 +155,8 @@ Project focused on work around C/C++ headers (C++ especially). Feel free to fork
Third Party libraries
----------------------

* LLVM 16.0.4 - our main backend of C++ analysis
* Boost 1.81.0 - python support & process launcher
* FMT - string formatter
* LLVM - our main backend of C++ analysis
* Boost.Python - python support & process launcher
* fmt - string formatter
* googletest - for internal unit testing
* pytest - for python side unit testing
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <clang/Frontend/FrontendActions.h>
#include <RG3/LLVM/CodeEvaluator.h>
#include <unordered_map>
#include <unordered_set>
#include <string>



namespace rg3::llvm::actions
{
struct CollectConstexprVariableEvalResultAction : public clang::ASTFrontendAction
{
std::unordered_set<std::string> aExpectedVariables {};
std::unordered_map<std::string, VariableValue>* pEvaluatedVariables { nullptr };

std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(clang::CompilerInstance& /*compilerInstance*/, clang::StringRef /*file*/) override;
};
}
46 changes: 46 additions & 0 deletions LLVM/include/RG3/LLVM/CodeEvaluator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include <RG3/LLVM/CompilerConfigDetector.h>
#include <RG3/LLVM/CompilerConfig.h>
#include <RG3/LLVM/CodeAnalyzer.h>

#include <boost/noncopyable.hpp>

#include <unordered_map>
#include <optional>
#include <variant>
#include <cstdint>
#include <string>
#include <vector>


namespace rg3::llvm
{
using VariableValue = std::variant<bool, std::int64_t, std::uint64_t, float, double, std::string>;

struct CodeEvaluateResult
{
AnalyzerResult::CompilerIssuesVector vIssues;
std::unordered_map<std::string, VariableValue> mOutputs;

explicit operator bool() const noexcept;
};

class CodeEvaluator : public boost::noncopyable
{
public:
CodeEvaluator();
CodeEvaluator(CompilerConfig compilerConfig);

void setCompilerEnvironment(const CompilerEnvironment& env);
CompilerConfig& getCompilerConfig();
const CompilerConfig& getCompilerConfig() const;

CodeEvaluateResult evaluateCode(const std::string& sCode, const std::vector<std::string>& aCaptureOutputVariables);

private:
std::optional<CompilerEnvironment> m_env;
CompilerConfig m_compilerConfig;
std::string m_sSourceCode {};
};
}
23 changes: 23 additions & 0 deletions LLVM/include/RG3/LLVM/CompilerInstanceFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <clang/Frontend/CompilerInstance.h>

#include <RG3/LLVM/CompilerConfigDetector.h>
#include <RG3/LLVM/CompilerConfig.h>

#include <filesystem>
#include <variant>
#include <string>


namespace rg3::llvm
{
struct CompilerInstanceFactory
{
static void makeInstance(
clang::CompilerInstance* pOutInstance,
const std::variant<std::filesystem::path, std::string>& sInput,
const CompilerConfig& sCompilerConfig,
const CompilerEnvironment* pCompilerEnv = nullptr);
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include <clang/AST/ASTConsumer.h>
#include <RG3/LLVM/CodeEvaluator.h>
#include <unordered_map>
#include <unordered_set>
#include <string>


namespace rg3::llvm::consumers
{
struct CollectConstexprVariableEvalResult : public clang::ASTConsumer
{
std::unordered_set<std::string> aExpectedVariables {};
std::unordered_map<std::string, VariableValue>* pEvaluatedVariables { nullptr };

public:
CollectConstexprVariableEvalResult();

void HandleTranslationUnit(clang::ASTContext& ctx) override;
};
}
15 changes: 15 additions & 0 deletions LLVM/source/Actions/CollectConstexprVariableEvalResultAction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <RG3/LLVM/Actions/CollectConstexprVariableEvalResultAction.h>
#include <RG3/LLVM/Consumers/CollectConstexprVariableEvalResult.h>


namespace rg3::llvm::actions
{
std::unique_ptr<clang::ASTConsumer> CollectConstexprVariableEvalResultAction::CreateASTConsumer(clang::CompilerInstance&, clang::StringRef)
{
auto pConsumer = std::make_unique<consumers::CollectConstexprVariableEvalResult>();
pConsumer->aExpectedVariables = aExpectedVariables;
pConsumer->pEvaluatedVariables = pEvaluatedVariables;

return std::move(pConsumer);
}
}
Loading