Skip to content

Commit

Permalink
Dont require __main__ for an addon (danmar#3363)
Browse files Browse the repository at this point in the history
  • Loading branch information
pfultz2 authored Aug 12, 2021
1 parent d86ff32 commit 2a36571
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 35 deletions.
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ if (BUILD_TESTS)
enable_testing()
endif()

add_custom_target(copy_cfg ALL
${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/cfg"
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/cfg"
COMMENT "Copying cfg files to ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}")

add_custom_target(copy_addons ALL
${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/addons"
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/addons"
COMMENT "Copying addons files to ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}")

if(USE_BUNDLED_TINYXML2)
message(STATUS "Using bundled version of tinyxml2")
add_subdirectory(externals/tinyxml2)
Expand Down
39 changes: 39 additions & 0 deletions addons/cppcheck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

import cppcheckdata, sys, os

__checkers__ = []

def checker(f):
__checkers__.append(f)
return f


__errorid__ = ''
__addon_name__ = ''
def reportError(location, severity, message, errorId=None):
cppcheckdata.reportError(location, severity, message, __addon_name__, errorId or __errorid__)

def runcheckers():
# If there are no checkers then dont run
if len(__checkers__) == 0:
return
global __addon_name__
global __errorid__
addon = sys.argv[0]
parser = cppcheckdata.ArgumentParser()
args = parser.parse_args()

__addon_name__ = os.path.splitext(os.path.basename(addon))[0]

for dumpfile in args.dumpfile:
if not args.quiet:
print('Checking %s...' % dumpfile)

data = cppcheckdata.CppcheckData(dumpfile)

for cfg in data.iterconfigurations():
if not args.quiet:
print('Checking %s, config %s...' % (dumpfile, cfg.name))
for c in __checkers__:
__errorid__ = c.__name__
c(cfg, data)
51 changes: 21 additions & 30 deletions addons/findcasts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,31 @@
# Locate casts in the code
#

import cppcheckdata
import cppcheck
import sys

for arg in sys.argv[1:]:
if arg.startswith('-'):
continue
@cppcheck.checker
def cast(cfg, data):
for token in cfg.tokenlist:
if token.str != '(' or not token.astOperand1 or token.astOperand2:
continue

print('Checking %s...' % arg)
data = cppcheckdata.CppcheckData(arg)
# Is it a lambda?
if token.astOperand1.str == '{':
continue

for cfg in data.iterconfigurations():
print('Checking %s, config %s...' % (arg, cfg.name))
for token in cfg.tokenlist:
if token.str != '(' or not token.astOperand1 or token.astOperand2:
continue
# we probably have a cast.. if there is something inside the parentheses
# there is a cast. Otherwise this is a function call.
typetok = token.next
if not typetok.isName:
continue

# Is it a lambda?
if token.astOperand1.str == '{':
continue
# cast number => skip output
if token.astOperand1.isNumber:
continue

# we probably have a cast.. if there is something inside the parentheses
# there is a cast. Otherwise this is a function call.
typetok = token.next
if not typetok.isName:
continue
# void cast => often used to suppress compiler warnings
if typetok.str == 'void':
continue

# cast number => skip output
if token.astOperand1.isNumber:
continue

# void cast => often used to suppress compiler warnings
if typetok.str == 'void':
continue

cppcheckdata.reportError(token, 'information', 'found a cast', 'findcasts', 'cast')

sys.exit(cppcheckdata.EXIT_CODE)
cppcheck.reportError(token, 'information', 'found a cast')
12 changes: 12 additions & 0 deletions addons/runaddon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import cppcheckdata, cppcheck, runpy, sys, os

if __name__ == '__main__':
addon = sys.argv[1]
__addon_name__ = os.path.splitext(os.path.basename(addon))[0]
sys.argv.pop(0)

runpy.run_path(addon, run_name='__main__')

# Run registered checkers
cppcheck.runcheckers()
sys.exit(cppcheckdata.EXIT_CODE)
3 changes: 3 additions & 0 deletions cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ if(tinyxml2_FOUND AND NOT USE_BUNDLED_TINYXML2)
target_link_libraries(cppcheck tinyxml2)
endif()

add_dependencies(cppcheck copy_cfg)
add_dependencies(cppcheck copy_addons)

install(TARGETS cppcheck
RUNTIME DESTINATION ${CMAKE_INSTALL_FULL_BINDIR}
COMPONENT applications)
Expand Down
7 changes: 6 additions & 1 deletion lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "version.h"

#include "exprengine.h"
#include <string>

#define PICOJSON_USE_INT64
#include <picojson.h>
Expand Down Expand Up @@ -71,6 +72,7 @@ namespace {
std::string args;
std::string python;
bool ctu = false;
std::string runScript{};

static std::string getFullPath(const std::string &fileName, const std::string &exename) {
if (Path::fileExists(fileName))
Expand Down Expand Up @@ -153,6 +155,8 @@ namespace {
pos2 = std::string::npos;
name = scriptFile.substr(pos1, pos2 - pos1);

runScript = getFullPath("runaddon.py", exename);

return "";
}

Expand Down Expand Up @@ -291,7 +295,8 @@ static std::string executeAddon(const AddonInfo &addonInfo,
}

const std::string fileArg = (endsWith(file, FILELIST, sizeof(FILELIST)-1) ? " --file-list " : " ") + cmdFileName(file);
const std::string args = cmdFileName(addonInfo.scriptFile) + " --cli" + addonInfo.args + fileArg;
const std::string args =
cmdFileName(addonInfo.runScript) + " " + cmdFileName(addonInfo.scriptFile) + " --cli" + addonInfo.args + fileArg;

std::string result;
if (!executeCommand(pythonExe, split(args), redirect, &result))
Expand Down
5 changes: 1 addition & 4 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,8 @@ if (BUILD_TESTS)
target_precompile_headers(testrunner PRIVATE precompiled.h)
endif()

add_custom_target(copy_cfg ALL
${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/cfg"
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/cfg"
COMMENT "Copying cfg files to ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}")
add_dependencies(testrunner copy_cfg)
add_dependencies(testrunner copy_addons)

if (LIBXML2_XMLLINT_EXECUTABLE)
# TODO: get rid of the copy
Expand Down

0 comments on commit 2a36571

Please sign in to comment.