Skip to content

Commit

Permalink
Enable linting for api/tests and doc/spec
Browse files Browse the repository at this point in the history
  • Loading branch information
jonrecker authored and mav-intel committed Dec 19, 2023
1 parent 371864b commit 3744894
Show file tree
Hide file tree
Showing 25 changed files with 271 additions and 250 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
fail_fast: false
default_language_version:
python: python3
exclude: '/ext/|doc/spec/|api/tests/'
exclude: '/ext/'
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.5.0
Expand Down
48 changes: 30 additions & 18 deletions api/tests/abi/abi_test_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,72 +3,84 @@
# #
# # SPDX-License-Identifier: MIT
# ############################################################################*/

"""
Generate ABI tests.
"""
# pylint: disable=invalid-name

# To run this script:
# 1. Install clang
# 2. Install clang python binding (pip3 install clang)
# 3. export LD_LIBRARY_PATH=<path to clang SO lib> (Macos --> LD_LIBRARY_PATH=/Library/Developer/CommandLineTools/usr/lib)
# 3. export LD_LIBRARY_PATH=<path to clang SO lib>

import argparse
import os
import glob
import re
import sys
import clang.cindex

ignore_list = ['mfxEncryptedData', 'mfxSyncPoint', 'mfxSession', 'mfxLoader', 'mfxConfig']
ignore_list = [
'mfxEncryptedData', 'mfxSyncPoint', 'mfxSession', 'mfxLoader', 'mfxConfig'
]

structures = {}


def find_structs(node, mfxStruct):
"""Find structures in the headers"""
if node.kind == clang.cindex.CursorKind.TYPEDEF_DECL:
sp = node.underlying_typedef_type.spelling
if sp.startswith("struct"): # this is trick. Don't know right way to assotiate typedef with referenced node
if sp.startswith(
"struct"
): # this is trick. Don't know right way to associate typedef with referenced node
structures[node.displayname] = node.displayname
if mfxStruct in sp:
# print(type(node))
# print(node.underlying_typedef_type.kind)
fields = []
for cc in node.walk_preorder() :
for cc in node.walk_preorder():
if cc.kind == clang.cindex.CursorKind.FIELD_DECL:
if("reserved" not in cc.spelling):
if "reserved" not in cc.spelling:
fields.append(cc.spelling)

with open("gen__.c", 'w') as gf:
with open("gen__.c", 'w', encoding="utf-8") as gf:
original_stdout = sys.stdout
sys.stdout = gf
print("#include <stddef.h>")
print("#include <stdio.h>")
print("#include <mfx.h>")
print("int main() {")
print("printf(\"MSDK_STATIC_ASSERT_STRUCT_SIZE(" + mfxStruct + ", %lu)\\n\", sizeof(" + mfxStruct + "));")
print("printf(\"MSDK_STATIC_ASSERT_STRUCT_SIZE(" + mfxStruct +
", %lu)\\n\", sizeof(" + mfxStruct + "));")
for f in fields:
# MSDK_STATIC_ASSERT_STRUCT_OFFSET(mfxInitializationParam, AccelerationMode ,0)
print("printf(\" MSDK_STATIC_ASSERT_STRUCT_OFFSET(" + mfxStruct + ", " + f + ", %lu)\\n\", offsetof(" + mfxStruct + ", " + f + "));")
print(
"printf(\" MSDK_STATIC_ASSERT_STRUCT_OFFSET(" +
mfxStruct + ", " + f + ", %lu)\\n\", offsetof(" +
mfxStruct + ", " + f + "));")
print("return 0;}")
sys.stdout = original_stdout

print("Done")
exit(0)
sys.exit(0)
# Recurse for children of this node
for c in node.get_children():
find_structs(c, mfxStruct)


parser = argparse.ArgumentParser(description='Searches for the structs with missed ABI backward compatibility tests.')
parser = argparse.ArgumentParser(
description=
'Searches for the structs with missed ABI backward compatibility tests.')
parser.add_argument('-i', '--ifolder', help='Input folder with header files.')
parser.add_argument('-s', '--structure', help='Name of the structure.')

args = parser.parse_args()

errCount = 0
state = 'search'
for filename in glob.glob(os.path.join(args.ifolder,'*.h')):
print ("Parsing of " + filename)
for filename in glob.glob(os.path.join(args.ifolder, '*.h')):
print("Parsing of " + filename)
index = clang.cindex.Index.create()
tu = index.parse(filename, ["-DONEVPL_EXPERIMENTAL"])
find_structs(tu.cursor, args.structure)

print("Structure definition wasn't located: " + args.structure)
exit(-1)
sys.exit(-1)
33 changes: 22 additions & 11 deletions api/tests/abi/check_missing_structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
# #
# # SPDX-License-Identifier: MIT
# ############################################################################*/

"""
Check for structures which are not present in ABI tests.
"""
# pylint: disable=invalid-name

# To run this script:
# 1. Install clang
# 2. Install clang python binding (pip3 install clang)
# 3. export LD_LIBRARY_PATH=<path to clang SO lib> (Macos --> LD_LIBRARY_PATH=/Library/Developer/CommandLineTools/usr/lib)
# 3. export LD_LIBRARY_PATH=<path to clang SO lib>

import argparse
import os
Expand All @@ -17,43 +20,51 @@
import sys
import clang.cindex

ignore_list = ['mfxEncryptedData', 'mfxSyncPoint', 'mfxSession', 'mfxLoader', 'mfxConfig']
ignore_list = [
'mfxEncryptedData', 'mfxSyncPoint', 'mfxSession', 'mfxLoader', 'mfxConfig'
]

structures = {}


def find_structs(node):
"""Find structures in the headers"""
if node.kind == clang.cindex.CursorKind.TYPEDEF_DECL:
sp = node.underlying_typedef_type.spelling
if sp.startswith("struct"): # this is trick. Don't know right way to assotiate typedef with referenced node
if sp.startswith(
"struct"
): # this is trick. Don't know right way to assotiate typedef with referenced node
structures[node.displayname] = node.displayname
# Recurse for children of this node
for c in node.get_children():
find_structs(c)


parser = argparse.ArgumentParser(description='Searches for the structs with missed ABI backward compatibility tests.')
parser = argparse.ArgumentParser(
description=
'Searches for the structs with missed ABI backward compatibility tests.')
parser.add_argument('-i', '--ifolder', help='Input folder with header files.')
parser.add_argument('-t', '--test_file', help='Input file with ABI tests.')

args = parser.parse_args()

errCount = 0
state = 'search'
for filename in glob.glob(os.path.join(args.ifolder,'*.h')):
print ("Parsing of " + filename)
for filename in glob.glob(os.path.join(args.ifolder, '*.h')):
print("Parsing of " + filename)
index = clang.cindex.Index.create()
tu = index.parse(filename, ["-DONEVPL_EXPERIMENTAL"])
find_structs(tu.cursor)

print(structures)
print("Found structures: {}".format(len(structures)))

print ("Parsing of test file: " + args.test_file)
print("Parsing of test file: " + args.test_file)

with open(args.test_file, 'r') as file:
with open(args.test_file, 'r', encoding="utf-8") as file:
data = file.read()
for s in structures:
if not re.search(s,data):
if not re.search(s, data):
if s not in ignore_list:
print("struct {} missed".format(s))
errCount = errCount + 1
Expand All @@ -63,4 +74,4 @@ def find_structs(node):
else:
print("Failed with {} errors.".format(errCount))

exit(-errCount)
sys.exit(-errCount)
105 changes: 67 additions & 38 deletions api/tests/abi/check_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,93 @@
# #
# # SPDX-License-Identifier: MIT
# ############################################################################*/
"""
Check for expected structure packing.
"""
# pylint: disable=invalid-name

import argparse
import os
import glob
import re
import sys

typedefs_with_ptr_list = ['mfxHDL', 'mfxThreadTask', 'mfxEncryptedData', 'mfxSyncPoint', 'mfxSession', 'mfxLoader', 'mfxConfig']
typedefs_with_ptr_list = [
'mfxHDL', 'mfxThreadTask', 'mfxEncryptedData', 'mfxSyncPoint',
'mfxSession', 'mfxLoader', 'mfxConfig'
]

def CheckPtr(line):
newline = re.sub(r" *", '', line) # trim spaces. can be embedded into regexp below but will meake it more complex for reader
result = re.search(r"\S+\*+\S+;",newline) # search for the pointer declaration like "xxx*yyy;"
if result:
return 1
return 0

def CheckTypedefWithPtr(line):
for typedef in typedefs_with_ptr_list:
result = re.search(typedef,newline) # search for the field with the type defines through the typedef
if result:
return 1
return 0
def CheckPtr(p_line):
"""Check pointer"""
p_newline = re.sub(
r" *", '', p_line
) # trim spaces. can be embedded into regexp below but will meake it more complex for reader
p_result = re.search(
r"\S+\*+\S+;",
p_newline) # search for the pointer declaration like "xxx*yyy;"
if p_result:
return 1
return 0


def CheckTypedefWithPtr(_line):
"""Check typedef"""
for typedef in typedefs_with_ptr_list:
p_result = re.search(
typedef, newline
) # search for the field with the type defines through the typedef
if p_result:
return 1
return 0


checkers = [CheckPtr, CheckTypedefWithPtr]

parser = argparse.ArgumentParser(description='Check that structures with pointers have correct pragme pack.')
parser = argparse.ArgumentParser(
description='Check that structures with pointers have correct pragme pack.'
)
parser.add_argument('-i', '--ifolder', help='Input folder with header files.')

args = parser.parse_args()

errCount = 0
state = 'search'
for filename in glob.glob(os.path.join(args.ifolder,'*.h')):
with open(filename, 'r') as f:
print ("Parsing of " + filename)
Lines = f.readlines()
for line in Lines:
result = re.search(r"^\s*MFX_PACK_BEGIN_USUAL_STRUCT", line)
if result and state == "search":
state = 'inmacro'
continue
result = re.search(r"^\s*MFX_PACK_END", line)
if result and state == "inmacro":
state = 'search'
continue

if state == 'inmacro':
newline = re.sub(r"\/\*.*\*\/", '', line) # remove /* */ comment within the single line
newline = re.sub(r"/\*.*", '', newline) # remove begin of the comment when we have multi line comments
newline = re.sub(r".*\*\/$", '', newline) # remove end of the comment when we have multi line comments
for checker in checkers:
r = checker(newline)
if(r):
print("Line{}: {}".format(errCount, newline.strip())) # wrong pack of the structure is found. structure with pointers is wrongly packed.
errCount += 1
for filename in glob.glob(os.path.join(args.ifolder, '*.h')):
with open(filename, 'r', encoding="utf-8") as f:
print("Parsing of " + filename)
Lines = f.readlines()
for line in Lines:
result = re.search(r"^\s*MFX_PACK_BEGIN_USUAL_STRUCT", line)
if result and state == "search":
state = 'inmacro'
continue
result = re.search(r"^\s*MFX_PACK_END", line)
if result and state == "inmacro":
state = 'search'
continue

if state == 'inmacro':
newline = re.sub(
r"\/\*.*\*\/", '',
line) # remove /* */ comment within the single line
newline = re.sub(
r"/\*.*", '', newline
) # remove begin of the comment when we have multi line comments
newline = re.sub(
r".*\*\/$", '', newline
) # remove end of the comment when we have multi line comments
for checker in checkers:
r = checker(newline)
if r:
print(
"Line{}: {}".format(errCount, newline.strip())
) # wrong pack is found. structure with pointers is wrongly packed.
errCount += 1

if errCount == 0:
print("Passed")
else:
print("Failed with {} errors.".format(errCount))

exit(-errCount)
sys.exit(-errCount)
4 changes: 2 additions & 2 deletions api/tests/abi/mfx_static_assert_structs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include <stddef.h>
#include <stdio.h>

#include "mfx.h"
#include "mfxcamera.h"
#include "./mfx.h"
#include "./mfxcamera.h"

/* .cpp instead of .h to avoid changing of include files dependencies graph
and not to include unnecessary includes into libmfx library */
Expand Down
8 changes: 4 additions & 4 deletions api/tests/compile_headers/removed_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
* removed API entry - please keep original name of the header file.
*/

#ifndef __REMOVED_API_H__
#define __REMOVED_API_H__
#ifndef API_TESTS_COMPILE_HEADERS_REMOVED_API_H_
#define API_TESTS_COMPILE_HEADERS_REMOVED_API_H_

#include "mfxstructures.h"
#include "./mfxstructures.h"

/* Don't expose this file publically without special approval. */

Expand All @@ -36,4 +36,4 @@ enum {

/* Functions*/

#endif /* __REMOVED_API_H__ */
#endif /* API_TESTS_COMPILE_HEADERS_REMOVED_API_H_ */
6 changes: 6 additions & 0 deletions api/tests/compile_headers/sdk_headers.inc
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*############################################################################
# Copyright (C) 2020 Intel Corporation
#
# SPDX-License-Identifier: MIT
############################################################################*/

#pragma message ("Add headers to test begin")

#include "mfx.h"
Expand Down
2 changes: 1 addition & 1 deletion api/tests/compile_headers/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include <stddef.h>

#include "removed_api.h"
#include "./removed_api.h"

#include "sdk_headers.inc"

Expand Down
4 changes: 2 additions & 2 deletions api/tests/compile_headers/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ typedef struct {
static_assert(offsetof(PackStart, a) == 0, "Error - offset of PackStart.a should be 0");
static_assert(offsetof(PackStart, b) == 1, "Error - offset of PackStart.b should be 1");

#include "removed_api.h"
#include "./removed_api.h"

#include "mfxvideo++.h"
#include "./mfxvideo++.h"

#include "sdk_headers.inc"

Expand Down
Loading

0 comments on commit 3744894

Please sign in to comment.