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

Docker support2 #28

Merged
merged 47 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
40dbddb
new: separate rules from oclint; tester.py
jcdkiki Dec 28, 2023
f3702e9
new: reset oclint to 22.02
jcdkiki Dec 28, 2023
30cb8b0
new: change ex3 to c++
jcdkiki Dec 28, 2023
ec8ad1d
new: rm ex3/main.c; TooLongIfSequenceRule; MAX_IF_SEQUENCE_LEN
jcdkiki Dec 28, 2023
795ba9a
add .oclint to gitignore
jcdkiki Jan 1, 2024
2ba6a7b
fix: MAX_IF_SEQUENCE_LEN
jcdkiki Jan 1, 2024
bbe0a0d
add compile_commands.json to gitignore
jcdkiki Jan 1, 2024
248608f
new: fix build+install
jcdkiki Jan 1, 2024
750322c
new: readme --> wiki
jcdkiki Jan 1, 2024
b68cf37
compilation type as argument
jcdkiki Feb 2, 2024
247e091
bash scripts for examples
jcdkiki Feb 2, 2024
3082ea7
add clean to example #4 makefile
jcdkiki Feb 2, 2024
50a1be7
add install-deps.sh script
jcdkiki Feb 3, 2024
5a954c1
rm tester.py // feels useless
jcdkiki Feb 3, 2024
087b3d0
copy OCLint bin files to /usr/local/bin when install
jcdkiki Feb 3, 2024
867c514
add commit hash variable
jcdkiki Feb 3, 2024
c262811
brief README
jcdkiki Feb 3, 2024
2c5a7d3
fix typo README.md
jcdkiki Feb 4, 2024
dd7bcab
help scripts for building+deps
jcdkiki Feb 7, 2024
2b544d8
better examples
jcdkiki Feb 7, 2024
6ca0dac
fix test-gcc???
jcdkiki Feb 9, 2024
8012e75
goto example
jcdkiki Feb 9, 2024
207270c
add MallocSizeofRule + example
jcdkiki Feb 9, 2024
8dfd636
Merge branch 'new' of github.com:moevm/oclint_extensions into new
jcdkiki Feb 9, 2024
c0605c6
fix dumb thing
jcdkiki Feb 9, 2024
1b8082f
ex-sizeof: add int forgot_parens()
jcdkiki Feb 9, 2024
8aa72e4
char* --> char
jcdkiki Feb 9, 2024
84c7ef1
add StringCompareRule + example
jcdkiki Feb 10, 2024
96b7dc8
clean includes in StringCompareRule.cpp
jcdkiki Feb 10, 2024
df60ab7
StringCompareRule: add support for wchar_t + wchar_t example
jcdkiki Feb 21, 2024
b59d312
install-oclint.sh: add /usr/bin/bash
jcdkiki Feb 24, 2024
6abfea9
GlobalVariableRule + example
jcdkiki Feb 26, 2024
086b632
GlobalVariableRule: cleanup includes
jcdkiki Feb 26, 2024
61383af
OnlyMainFunctionRule + examples
jcdkiki Feb 27, 2024
a5fee9d
OnlyMainFunctionRule: add explicit return from non-void func
jcdkiki Feb 27, 2024
ee4e891
Makefile for build/install/uninstall/examples
jcdkiki Mar 14, 2024
c35c97c
fix readme + separate test-gcc and test-makefile
jcdkiki Mar 14, 2024
55caf7e
new: TooLongIfSequenceRule. compare conditions
jcdkiki Mar 19, 2024
f922e1a
ex-nested-function example
jcdkiki Apr 27, 2024
0748849
StringCompareRule: add NULL check
jcdkiki May 14, 2024
ec3ef5a
docker support that actually works (at least locally on my pc)
jcdkiki Jul 6, 2024
6a694a3
docker_support2: fix dockerfile + fix chmod error?
jcdkiki Jul 9, 2024
fdd87aa
docker_support2: fix apt update + add makefile support
jcdkiki Jul 9, 2024
a0e50f7
small fixes
jcdkiki Jul 19, 2024
ae20683
newlines (editorconfig)
jcdkiki Jul 19, 2024
4fc9a78
fix namespaces
jcdkiki Jul 19, 2024
4829f64
better examples to readme
jcdkiki Jul 20, 2024
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
oclint
build
.oclint
compile_commands.json
146 changes: 146 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
CMAKE_MINIMUM_REQUIRED(VERSION 2.13.4)
PROJECT(OCLINT_EXTENSIONS)

SET(LLVM_ROOT ${CMAKE_SOURCE_DIR}/oclint/build/llvm-install)

SET(CMAKE_DISABLE_SOURCE_CHANGES ON)
SET(CMAKE_DISABLE_IN_SOURCE_BUILD ON)
set(CMAKE_MACOSX_RPATH ON)
SET(CMAKE_BUILD_TYPE None)

IF(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
SET(CMAKE_CXX_FLAGS "-fcolor-diagnostics")
ENDIF()
SET(CMAKE_CXX_FLAGS "-std=c++14 ${CMAKE_CXX_LINKER_FLAGS} -fno-rtti -fPIC ${CMAKE_CXX_FLAGS}")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_CXX_LINKER_FLAGS} -fno-rtti")

IF(OCLINT_BUILD_TYPE STREQUAL "Release")
SET(CMAKE_CXX_FLAGS "-O3 -DNDEBUG ${CMAKE_CXX_FLAGS}")
SET(CMAKE_SHARED_LINKER_FLAGS "-s ${CMAKE_SHARED_LINKER_FLAGS}")
ELSE()
SET(CMAKE_CXX_FLAGS "-O0 -g ${CMAKE_CXX_FLAGS}")
SET(CMAKE_SHARED_LINKER_FLAGS "-g ${CMAKE_SHARED_LINKER_FLAGS}")
ENDIF()

SET(OCLINT_VERSION_RELEASE "22.02")

IF(NOT EXISTS ${LLVM_ROOT}/include/llvm)
MESSAGE(FATAL_ERROR "LLVM_ROOT (${LLVM_ROOT}) is not a valid LLVM install. Could not find ${LLVM_ROOT}/include/llvm")
ENDIF()
MESSAGE("LLVM_ROOT: ${LLVM_ROOT}")
IF(EXISTS ${LLVM_ROOT}/lib/cmake/llvm)
SET(LLVM_DIR ${LLVM_ROOT}/lib/cmake/llvm)
ELSE()
SET(LLVM_DIR ${LLVM_ROOT}/share/llvm/cmake)
ENDIF()
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${LLVM_DIR}")
INCLUDE(LLVMConfig)

INCLUDE_DIRECTORIES( ${LLVM_INCLUDE_DIRS} )
LINK_DIRECTORIES( ${LLVM_LIBRARY_DIRS} )
ADD_DEFINITIONS( ${LLVM_DEFINITIONS} )

STRING(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" LLVM_VERSION_RELEASE ${LLVM_PACKAGE_VERSION})

MESSAGE(STATUS "Found LLVM LLVM_PACKAGE_VERSION: ${LLVM_PACKAGE_VERSION} - LLVM_VERSION_RELEASE: ${LLVM_VERSION_RELEASE}")
MESSAGE(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
LLVM_MAP_COMPONENTS_TO_LIBNAMES(REQ_LLVM_LIBRARIES asmparser bitreader instrumentation mcparser option support frontendopenmp)

SET(CLANG_LIBRARIES
clangToolingCore
clangTooling
clangFrontend
clangDriver
clangSerialization
clangParse
clangSema
clangAnalysis
clangEdit
clangASTMatchers
clangAST
clangLex
clangBasic)

IF(TEST_BUILD)
ENABLE_TESTING()
IF(NOT APPLE)
ADD_DEFINITIONS(
--coverage
)
ENDIF()

INCLUDE_DIRECTORIES(
${GOOGLETEST_SRC}/googlemock/include
${GOOGLETEST_SRC}/googletest/include
)
LINK_DIRECTORIES(
${GOOGLETEST_BUILD}
${GOOGLETEST_BUILD}/lib
)
SET(GTEST_LIBS gmock gtest)

# Find CUDA
FIND_PROGRAM(NVIDIA_NVCC_BIN "nvcc")
IF (NVIDIA_NVCC_BIN)
MESSAGE(STATUS "Enable tests for CUDA rules.")
SET(TEST_CUDA TRUE)
ELSE()
SET(TEST_CUDA FALSE)
ENDIF()

# Setup the path for profile_rt library
STRING(TOLOWER ${CMAKE_SYSTEM_NAME} COMPILER_RT_SYSTEM_NAME)
LINK_DIRECTORIES(${LLVM_LIBRARY_DIRS}/clang/${LLVM_VERSION_RELEASE}/lib/${COMPILER_RT_SYSTEM_NAME})
IF(APPLE)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
ELSEIF(${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
SET(PROFILE_RT_LIBS clang_rt.profile-aarch64 --coverage)
ELSE()
SET(PROFILE_RT_LIBS clang_rt.profile-x86_64 --coverage)
ENDIF()
ENDIF()

IF(DOC_GEN_BUILD)
SET(CMAKE_CXX_FLAGS "-DDOCGEN ${CMAKE_CXX_FLAGS}")
ENDIF()




INCLUDE_DIRECTORIES(
${CMAKE_SOURCE_DIR}/oclint/oclint-core/include
${CMAKE_SOURCE_DIR}/oclint/oclint-metrics/include
${CMAKE_SOURCE_DIR}/oclint/oclint-rules/include
${CMAKE_SOURCE_DIR}/oclint/oclint-rules
)
LINK_DIRECTORIES(
${CMAKE_SOURCE_DIR}/oclint/build/oclint-core/lib
${CMAKE_SOURCE_DIR}/oclint/build/oclint-metrics/lib
${CMAKE_SOURCE_DIR}/oclint/build/oclint-rules/lib/helper
${CMAKE_SOURCE_DIR}/oclint/build/oclint-rules/lib/util
${CMAKE_SOURCE_DIR}/oclint/build/oclint-rules/lib
)

MACRO(build_dynamic_rule name)
ADD_LIBRARY(${name}Rule SHARED ${CMAKE_SOURCE_DIR}/rules/${name}Rule.cpp)

TARGET_LINK_LIBRARIES(${name}Rule OCLintAbstractRule)

TARGET_LINK_LIBRARIES(${name}Rule
clangASTMatchers
)

TARGET_LINK_LIBRARIES(${name}Rule
OCLintMetric
OCLintHelper
OCLintUtil
OCLintCore
)
install(TARGETS ${name}Rule DESTINATION lib/oclint/rules)
ENDMACRO(build_dynamic_rule)

BUILD_DYNAMIC_RULE(TooLongIfSequence)
BUILD_DYNAMIC_RULE(MallocSizeof)
BUILD_DYNAMIC_RULE(StringCompare)
BUILD_DYNAMIC_RULE(GlobalVariable)
BUILD_DYNAMIC_RULE(OnlyMainFunction)
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM ubuntu:22.04

RUN apt update && apt install -y ninja-build cmake bear g++ git python3 curl xz-utils zlib1g-dev
WORKDIR /app

COPY ./scripts/get_oclint.sh get_oclint.sh
RUN ./get_oclint.sh

COPY ./rules ./rules
COPY ./CMakeLists.txt .
COPY ./scripts/build_rules.sh ./scripts/build_rules.sh
RUN ./scripts/build_rules.sh

COPY . .

ENTRYPOINT ["scripts/entry.sh"]
46 changes: 8 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,21 @@
# oclint_extensions
# OCLint extensions

## Что это?

Кастомные правила для oclint. Список реализованных правил:
* слишком много последовательных конструкций if (TooManyConsecutiveIfStatementsRule)

## Установка, сборка и использование

### Создание заготовок для новых правил внутри репозитория oclint

Для начала склонируйте куда-нибудь [репозиторий oclint](https://github.com/oclint/oclint).

Далее необходимо добавить "заготовки" для новых правил. Другими словами, необходимо создать исходники для новых правил и внести их в CMakeLists oclint'а. Новые правила относятся к группе "etu". Для этого выполните следующее:
## Как собрать и установить?

```bash
cd oclint-scripts
./scaffoldRule -t ASTVisitor -c etu -n"too many consecutive if statements" -p 2 TooManyConsecutiveIfStatementsRule
./scripts/build.sh
```

### Копирование исходников с новыми правилами в репозиторий oclint
Папка `examples` содержит примеры для проверки работоспособности критериев.

Из репозитория oclint_extensions выполните:
Примеры запуска проверки:

```bash
cp -r ./oclint-rules YOUR_OCLINT_DIRECTORY_GOES_HERE
docker run --rm -it -v ./examples/ex-goto:/app/solution:ro oclint_checker
```

### Сборка и установка
Можно передавать аргументы:

Вернитесь в репозиторий oclint. Выполните следующее:
```bash
cd oclint-scripts
./make -release # этот скрипт начнет скачивание oclint-json-compilation-database размером в 514 МБ. имейте в виду

cd ..
cd build/oclint-release
sudo cp ./bin/oclint* /usr/local/bin/
sudo cp -rp ./lib/* /usr/local/lib/
docker run --rm -it -v ./examples/ex-goto:/app/solution:ro oclint_checker --rule StringCompareRule --rule GotoStatementRule # проверяет код на эти два критерия
```

### Использование

```
oclint --rule=TooManyConsecutiveIfStatements examples/ex*.c
```

## P.S.

Очень странный способ установки всего этого. В теории можно сделать форк oclint'а, ноооооо сомнительно. Должен же быть какой-то более простой способ.
Критикуйте, предлагайте.
24 changes: 24 additions & 0 deletions examples/ex-global/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <iostream>
using namespace std;

int bad_glob_var = 0;

template<class T>
struct vec3 {
T x, y, z;
};

int main()
{
static int bad_static_var1 = 0, bad_static_var2 = 0;
static const int bad_static_const = 13;
static vec3<float> bad_vec { 0.f, 0.f, 0.f };

cin >> bad_static_var1 >> bad_static_var2 >> bad_glob_var;
cin >> bad_vec.x >> bad_vec.y >> bad_vec.z;

cout << bad_static_var1 * 2 << bad_static_var2 + bad_static_const << bad_glob_var / 4 << endl;
cout << bad_vec.x * 2 << bad_vec.y * 2 << bad_vec.z * 2 << endl;

return 0;
}
14 changes: 14 additions & 0 deletions examples/ex-goto/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <iostream>
using namespace std;

int main()
{
int x;
cin >> x;

if (x != 2) goto failed;
cout << "Congrats! You typed 2!" << endl;

failed:
return 0;
}
106 changes: 106 additions & 0 deletions examples/ex-if/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#include <stdio.h>

void simple_ifs()
{
int option = 0;
scanf("%d", &option);

if (option == 0) {
puts("Zero");
}
if (option == 1) {
puts("One");
}
if (option == 2) {
puts("Two");
}
if (option == 3) {
puts("Three");
}
if (option == 4) {
puts("Four");
}
if (option == 5) {
puts("Five");
}
if (option == 6) {
puts("Six");
}
if (option == 7) {
puts("Seven");
}
if (option == 8) {
puts("Eight");
}
if (option == 9) {
puts("Nine");
}
else {
puts("Something else");
}
}

void complex_ifs()
{
int x = 0;
int param1 = 0;
float param2 = 0.0;
float param3 = 0.0;
float param4 = 0.0;
int opt = 0;
int value = -1;
scanf("%d %d %f %f %f %d %d", &x, &param1, &param2, &param3, &param4, &opt, &value);

if (x == 0) {
puts("Zero");
}
else if (param1 > 100){
puts("Too large");
}
else if (param2 < 0.0){
puts("Negative");
}
else if( param3 * param2 > 1e+5){
puts("Invalid");
}
else if (param4 / 2 < param1){
puts("Unique case");
}
else if (opt < 0 || opt > 6){
puts("Invalid option");
}
else if (value == -1){
puts("By default");
}
}

int gcd(int a, int b);
int lcm(int a, int b);

void almost_complex_ifs()
{
int x, y;
scanf("%d %d", &x, &y);

// different function names
if (gcd(x, y) == 1) printf("GCD is 1\n");
else if (lcm(x, y) == 100) printf("LCM is 100\n");
else if (gcd(x, y) == 1337) printf("GCD is 1337\n");
else if (gcd(x, y) == 300) printf("GCD is 300\n");
if (lcm(x, y) == 512) printf("LCM is 512\n");
if (gcd(x, y) == 64) printf("GCD is 64\n");

// different binary operators
if (x > 100) printf("x is greater than 100\n");
else if (x < 0) printf("x is less than 0\n");
else if (x == 7) printf("x is 7\n");
else if (x == 9) printf("x is 9\n");
else if (x == 33) printf("x is 33\n");
else if (x == 6) printf("x is 6\n");
}

int main()
{
simple_ifs();
complex_ifs();
}
17 changes: 17 additions & 0 deletions examples/ex-nested-function/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <stdio.h>

int main()
{
int main_scope_int = 42;

void nested_func()
{
printf("NESTED FUNCTION %d\n", main_scope_int);
return;
}

printf("MAIN FUNCTION\n");
nested_func();

return 0;
}
Loading