Skip to content

Commit

Permalink
Fixed bug with error messages
Browse files Browse the repository at this point in the history
Diagnostics from `clang` is shown: unknown symbols are displayed
  • Loading branch information
kerim371 committed Oct 1, 2021
1 parent 82d06fd commit a948f54
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
15 changes: 8 additions & 7 deletions cppguts/dumpcpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
def get_diag_info(diag):
return {'severity': diag.severity,
'location': diag.location,
'category_name': diag.category_name,
'spelling': diag.spelling,
'ranges': diag.ranges,
'fixits': diag.fixits}
Expand Down Expand Up @@ -59,10 +60,10 @@ def get_node_info(node: Cursor, children = None) -> dict:
'lexical_parent.displayname': node.lexical_parent.displayname if node.lexical_parent else None,
'linkage': node.linkage,
'location': node.location,
'mangled_name': node.mangled_name,
'objc_type_encoding': node.objc_type_encoding,
'raw_comment': node.raw_comment,
'referenced': node.referenced,
# 'mangled_name': node.mangled_name if node.mangled_name else None,
# 'objc_type_encoding': node.objc_type_encoding,
# 'raw_comment': node.raw_comment,
# 'referenced': node.referenced,
'result_type spelling': node.result_type.spelling,
'semantic_parent.displayname': node.semantic_parent.displayname if node.semantic_parent else None,
'spelling': node.spelling,
Expand All @@ -71,8 +72,8 @@ def get_node_info(node: Cursor, children = None) -> dict:
'translation_unit spelling': node.translation_unit.spelling if node.translation_unit else None,
'type spelling': node.type.spelling,
# 'underlying_typedef_type spelling': node.underlying_typedef_type.spelling if node.underlying_typedef_type else None,
'walk_preorder': node.walk_preorder,
'xdata': node.xdata,
# 'walk_preorder': node.walk_preorder,
# 'xdata': node.xdata,
'children' : children}


Expand Down Expand Up @@ -121,7 +122,7 @@ def main():
if not tu:
parser.error("unable to load input")

pprint(('diags', [get_diag_info(d) for d in tu.diagnostics]))
pprint(('diagnostics:', [get_diag_info(d) for d in tu.diagnostics]))
if args.objname:
nodes_found = []
find_nodes(tu.cursor, nodes_found, args.objname)
Expand Down
35 changes: 26 additions & 9 deletions cppguts/editcpp.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import os, shutil, argparse, warnings
from os import listdir
from os.path import isfile, join
from pprint import pprint

from clang.cindex import Index
from clang.cindex import Cursor
from clang.cindex import CursorKind


def get_diag_info(diag):
return {'severity': diag.severity,
'location': diag.location,
'category_name': diag.category_name,
'spelling': diag.spelling,
'ranges': diag.ranges,
'fixits': diag.fixits}


def find_include_directives(txtdata: list) -> (list, list):
'''
Find lines and their indexes that has `#include` directives
Expand Down Expand Up @@ -81,16 +91,19 @@ def copy_file(srcfile: str, destfile: str) -> str:
def find_method_def_nodes(node: Cursor, nodes_found: list, location_filename=str()):
try:
if location_filename:
if (node.kind == CursorKind.CXX_METHOD or node.kind == CursorKind.FUNCTION_DECL and
node.is_definition() and
if (node.kind == CursorKind.CXX_METHOD and node.is_definition() and
os.path.samefile(node.location.file.name, location_filename)):
nodes_found.append(node)
elif (node.kind == CursorKind.FUNCTION_DECL and node.is_definition() and
os.path.samefile(node.location.file.name, location_filename)):
nodes_found.append(node)
else:
for child in node.get_children():
find_method_def_nodes(child, nodes_found, location_filename)
else:
if (node.kind == CursorKind.CXX_METHOD or node.kind == CursorKind.FUNCTION_DECL and
node.is_definition()):
if node.kind == CursorKind.CXX_METHOD and node.is_definition():
nodes_found.append(node)
elif node.kind == CursorKind.FUNCTION_DECL and node.is_definition():
nodes_found.append(node)
else:
for child in node.get_children():
Expand Down Expand Up @@ -215,17 +228,21 @@ def main():
if not tu_dest:
parser.error("clang unable to load destination file:\n{args.destfile}")

# print information about unknown files/functions/methods
pprint(('diagnostics in SOURCE:', [get_diag_info(d) for d in tu_src.diagnostics]))
pprint(('diagnostics in DESTINATION:', [get_diag_info(d) for d in tu_dest.diagnostics]))

method_def_nodes_src = []
find_method_def_nodes(tu_src.cursor, method_def_nodes_src, args.srcfile)
if not method_def_nodes_src:
parser.error(f"unable to find any method definition in source file:\n{args.srcfile}\n" +
"probably you forgot to pass `-std=c++03` (or higher) flag?")
f"probably you forgot to pass `-std=c++03` (or higher) flag?")

method_def_nodes_dest = []
find_method_def_nodes(tu_dest.cursor, method_def_nodes_dest, args.destfile)
if not method_def_nodes_dest:
parser.error(f"unable to find any function/method definition in destination file:\n{args.destfile}" +
"\nprobably you forgot to pass `-std=c++3` (or higher) flag?")
f"\nprobably you forgot to pass `-std=c++3` (or higher) flag?")

# read source file
with open(args.srcfile, mode='r') as file:
Expand All @@ -242,10 +259,10 @@ def main():
node_dest = find_method_matching_node(node_src, method_def_nodes_dest)
if not node_dest:
err_msg = (f"unable to find any destination function/method matching for a source function/method:\n" +
"\t{node_src.semantic_parent.displayname}::{node_src.spelling}->{node_src.type.spelling}\n" +
"found destination functions/methods:\n")
f"\t{node_src.semantic_parent.displayname}::{node_src.spelling}->{node_src.type.spelling}\n" +
f"found destination functions/methods:\n")
for node in method_def_nodes_dest:
err_msg += "\t{node.semantic_parent.displayname}::{node.spelling}->{node.type.spelling}\n"
err_msg += f"\t{node.semantic_parent.displayname}::{node.spelling}->{node.type.spelling}\n"
err_msg += f"also check is it definition? static? virtual? const?"
parser.error(err_msg)

Expand Down
6 changes: 2 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setuptools.setup(
name='cppguts',
version='1.0.1',
version='1.0.2',
packages=setuptools.find_packages(),
url='https://github.com/tierra-colada/cppguts',
license='MIT',
Expand All @@ -17,16 +17,14 @@
'automatically find and copy/paste new function definition',
long_description=long_description,
long_description_content_type='text/markdown',
download_url='https://github.com/tierra-colada/cppguts/archive/refs/tags/v1.0.1.tar.gz',
download_url='https://github.com/tierra-colada/cppguts/archive/refs/tags/v1.0.2.tar.gz',
classifiers=[
'Development Status :: 5 - Production/Stable',
'Environment :: Console',
'Intended Audience :: Developers',
'Topic :: Software Development :: Build Tools',
'Programming Language :: Python :: 3.6',
'Topic :: Software Development :: Code Generators',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
Expand Down

0 comments on commit a948f54

Please sign in to comment.