@@ -2869,3 +2869,170 @@ def test_ctu_builddir(tmp_path): # #11883
28692869 assert stderr .splitlines () == [
28702870 '{}:2:19: error: Null pointer dereference: p [ctunullpointer]' .format (test_file )
28712871 ]
2872+
2873+
2874+ def test_debug (tmp_path ):
2875+ test_file = tmp_path / 'test.c'
2876+ with open (test_file , "w" ) as f :
2877+ f .write (
2878+ """void f
2879+ {
2880+ (void)*((int*)0);
2881+ }
2882+ """ )
2883+
2884+ args = [
2885+ '-q' ,
2886+ '--debug' ,
2887+ str (test_file )
2888+ ]
2889+
2890+ exitcode , stdout , stderr = cppcheck (args )
2891+ assert exitcode == 0 , stdout
2892+ assert stdout .find ('##file ' ) != - 1
2893+ assert stdout .find ('##Value flow' ) != - 1
2894+ assert stdout .find ('### Symbol database ###' ) == - 1
2895+ assert stdout .find ('##AST' ) == - 1
2896+ assert stdout .find ('### Template Simplifier pass ' ) == - 1
2897+ assert stderr .splitlines () == []
2898+
2899+
2900+ def test_debug_xml (tmp_path ):
2901+ test_file = tmp_path / 'test.c'
2902+ with open (test_file , "w" ) as f :
2903+ f .write (
2904+ """void f
2905+ {
2906+ (void)*((int*)0);
2907+ }
2908+ """ )
2909+
2910+ args = [
2911+ '-q' ,
2912+ '--debug' ,
2913+ '--xml' ,
2914+ str (test_file )
2915+ ]
2916+
2917+ exitcode , stdout , stderr = cppcheck (args )
2918+ assert exitcode == 0 , stdout
2919+
2920+ assert stderr
2921+ assert ElementTree .fromstring (stderr ) is not None
2922+
2923+ assert stdout .find ('##file ' ) != - 1 # also exists in CDATA
2924+ assert stdout .find ('##Value flow' ) == - 1
2925+ assert stdout .find ('### Symbol database ###' ) == - 1
2926+ assert stdout .find ('##AST' ) == - 1
2927+ assert stdout .find ('### Template Simplifier pass ' ) == - 1
2928+
2929+ debug_xml = ElementTree .fromstring (stdout )
2930+ assert debug_xml is not None
2931+ assert debug_xml .tag == 'debug'
2932+ file_elem = debug_xml .findall ('file' )
2933+ assert len (file_elem ) == 1
2934+ valueflow_elem = debug_xml .findall ('valueflow' )
2935+ assert len (valueflow_elem ) == 1
2936+ scopes_elem = debug_xml .findall ('scopes' )
2937+ assert len (scopes_elem ) == 1
2938+ ast_elem = debug_xml .findall ('ast' )
2939+ assert len (ast_elem ) == 0
2940+
2941+
2942+ def test_debug_verbose (tmp_path ):
2943+ test_file = tmp_path / 'test.c'
2944+ with open (test_file , "w" ) as f :
2945+ f .write (
2946+ """void f
2947+ {
2948+ (void)*((int*)0);
2949+ }
2950+ """ )
2951+
2952+ args = [
2953+ '-q' ,
2954+ '--debug' ,
2955+ '--verbose' ,
2956+ str (test_file )
2957+ ]
2958+
2959+ exitcode , stdout , stderr = cppcheck (args )
2960+ assert exitcode == 0 , stdout
2961+ assert stdout .find ('##file ' ) != - 1
2962+ assert stdout .find ('##Value flow' ) != - 1
2963+ assert stdout .find ('### Symbol database ###' ) != - 1
2964+ assert stdout .find ('##AST' ) != - 1
2965+ assert stdout .find ('### Template Simplifier pass ' ) == - 1
2966+ assert stderr .splitlines () == []
2967+
2968+
2969+ def test_debug_verbose_xml (tmp_path ):
2970+ test_file = tmp_path / 'test.c'
2971+ with open (test_file , "w" ) as f :
2972+ f .write (
2973+ """void f
2974+ {
2975+ (void)*((int*)0);
2976+ }
2977+ """ )
2978+
2979+ args = [
2980+ '-q' ,
2981+ '--debug' ,
2982+ '--verbose' ,
2983+ '--xml' ,
2984+ str (test_file )
2985+ ]
2986+
2987+ exitcode , stdout , stderr = cppcheck (args )
2988+ assert exitcode == 0 , stdout
2989+
2990+ assert stderr
2991+ assert ElementTree .fromstring (stderr ) is not None
2992+
2993+ assert stdout .find ('##file ' ) != - 1 # also exists in CDATA
2994+ assert stdout .find ('##Value flow' ) == - 1
2995+ assert stdout .find ('### Symbol database ###' ) == - 1
2996+ assert stdout .find ('##AST' ) == - 1
2997+ assert stdout .find ('### Template Simplifier pass ' ) == - 1
2998+
2999+ debug_xml = ElementTree .fromstring (stdout )
3000+ assert debug_xml is not None
3001+ assert debug_xml .tag == 'debug'
3002+ file_elem = debug_xml .findall ('file' )
3003+ assert len (file_elem ) == 1
3004+ valueflow_elem = debug_xml .findall ('valueflow' )
3005+ assert len (valueflow_elem ) == 1
3006+ scopes_elem = debug_xml .findall ('scopes' )
3007+ assert len (scopes_elem ) == 1
3008+ ast_elem = debug_xml .findall ('ast' )
3009+ assert len (ast_elem ) == 1
3010+
3011+
3012+ # TODO: test with --xml
3013+ def test_debug_template (tmp_path ):
3014+ test_file = tmp_path / 'test.cpp'
3015+ with open (test_file , "w" ) as f :
3016+ f .write (
3017+ """template<class T> class TemplCl;
3018+ void f
3019+ {
3020+ (void)*((int*)0);
3021+ }
3022+ """ )
3023+
3024+ args = [
3025+ '-q' ,
3026+ '--debug' , # TODO: remove depdency on this
3027+ '--debug-template' ,
3028+ str (test_file )
3029+ ]
3030+
3031+ exitcode , stdout , stderr = cppcheck (args )
3032+ assert exitcode == 0 , stdout
3033+ assert stdout .find ('##file ' ) != - 1
3034+ assert stdout .find ('##Value flow' ) != - 1
3035+ assert stdout .find ('### Symbol database ###' ) == - 1
3036+ assert stdout .find ('##AST' ) == - 1
3037+ assert stdout .find ('### Template Simplifier pass ' ) != - 1
3038+ assert stderr .splitlines () == []
0 commit comments