Not able to print correct line numbers for then part of if statement, Clang -


in following code, parsing c-code using clang apis , trying

1)add html header , footer code, can viewed in browser

2)getting line number of part of if statement , printing it.

i having trouble in (2). not able make sense of output. following input (with line numbers added), getting (relevant) output

line number 6

line number 6

line number 6

line number 6


line number 12

line number 12

line number 12

line number 12


i expect 4 , 8 respectively. can explain wrong?

my input follows (please remove line numbers or go http://pastebin.com/hf5ymmaz)

%nl myinput.c

1 #include <stdio.h>   2 int func (int abc, int xyz) {  3     if (abc-1) 4     { 5         printf ("1\n"); 6     }   7      if (abc-2) 8     { 9         printf ("2\n"); 

10 }

11

12 return 0;

13 }

my code @ following location (only 1 file) http://pastebin.com/txf9bwa4

to compile code, run these commands:

clang_install=/usr/installdir/

g++ -i$clang_install/include/ -d_debug -d_gnu_source -d__stdc_constant_macros -d__stdc_format_macros -d__stdc_limit_macros -g -fvisibility-inlines-hidden -fno-exceptions -fno-rtti -fno-common -woverloaded-virtual -wcast-qual -fno-rtti -c -o rewritersample.o rewritersample.cpp

g++ -g -i$clang_install/include/ -o rewritersample rewritersample.o -lclangtooling -lclangfrontendtool -lclangfrontend -lclangdriver -lclangserialization -lclangcodegen -lclangparse -lclangsema -lclangstaticanalyzerfrontend -lclangstaticanalyzercheckers -lclangstaticanalyzercore -lclanganalysis -lclangarcmigrate -lclangrewritefrontend -lclangrewritecore -lclangedit -lclangast -lclanglex -lclangbasic -lllvmirreader -lllvmbitreader -lllvmasmparser -lllvmhexagoncodegen -lllvmhexagonasmprinter -lllvmhexagondesc -lllvmhexagoninfo -lllvmnvptxcodegen -lllvmnvptxdesc -lllvmnvptxinfo -lllvmnvptxasmprinter -lllvmmblazedisassembler -lllvmmblazecodegen -lllvmmblazedesc -lllvmmblazeasmprinter -lllvmmblazeasmparser -lllvmmblazeinfo -lllvmcppbackendcodegen -lllvmcppbackendinfo -lllvmmsp430codegen -lllvmmsp430desc -lllvmmsp430info -lllvmmsp430asmprinter -lllvmxcoredisassembler -lllvmxcorecodegen -lllvmxcoredesc -lllvmxcoreinfo -lllvmxcoreasmprinter -lllvmmipsdisassembler -lllvmmipscodegen -lllvmmipsasmparser -lllvmmipsdesc -lllvmmipsinfo -lllvmmipsasmprinter -lllvmarmdisassembler -lllvmarmcodegen -lllvmarmasmparser -lllvmarmdesc -lllvmarminfo -lllvmarmasmprinter -lllvmaarch64disassembler -lllvmaarch64codegen -lllvmaarch64asmparser -lllvmaarch64desc -lllvmaarch64info -lllvmaarch64asmprinter -lllvmaarch64utils -lllvmsparccodegen -lllvmsparcdesc -lllvmsparcinfo -lllvmtablegen -lllvmdebuginfo -lllvmoption -lllvmx86disassembler -lllvmx86asmparser -lllvmx86codegen -lllvmselectiondag -lllvmasmprinter -lllvmx86desc -lllvmx86info -lllvmx86asmprinter -lllvmx86utils -lllvmmcdisassembler -lllvmmcparser -lllvminstrumentation -lllvminterpreter -lllvmipo -lllvmvectorize -lllvmlinker -lllvmbitwriter -lllvmmcjit -lllvmjit -lllvmcodegen -lllvmobjcarcopts -lllvmscalaropts -lllvminstcombine -lllvmtransformutils -lllvmipa -lllvmanalysis -lllvmruntimedyld -lllvmexecutionengine -lllvmtarget -lllvmmc -lllvmobject -lllvmcore -lllvmsupport -l$clang_install/lib/ -lz -lpthread -lm -lllvmcppbackendcodegen -lllvmcppbackendinfo -lllvmtarget -lllvmcore -lllvmmc -lllvmobject -lllvmsupport -ldl

to run code, command is

$./rewritersample ./myinput.c

there 2 problems here:

  1. you're calling getlocstart on ifstatement, not on then, you're getting starting location of if token, not of { in if body.
  2. nl assigns numbers non-blank lines default. in http://pastebin.com/hf5ymmaz, clear if statements start on line 6 , line 12, not on lines numbered 3 , 7 in question. use nl -ba lines numbered correctly.

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -