Frage

In the following code, I am parsing a C-code using clang APIs and then trying to

1)Add a HTML header and footer to the code, so it can be viewed in a browser

2)Getting the line number of THEN part of an IF statement and printing it.

I am having trouble in (2). I am not able to make sense of the output. For the following input (with line numbers added), I am getting (relevant) output as

Line number is 6

Line number is 6

Line number is 6

Line number is 6


Line number is 12

Line number is 12

Line number is 12

Line number is 12


I would expect it to be 4 and 8 respectively. Can someone explain where I am wrong?

My input is as follows (Please remove the line numbers or go to 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 is at the following location (only one file) http://pastebin.com/txF9Bwa4

To compile the 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 the code, command is

$./rewritersample ./myinput.c

War es hilfreich?

Lösung

There are two problems here:

  1. You're calling getLocStart on IfStatement, not on Then, so you're getting the starting location of the if token, not of the { in the if body.
  2. nl only assigns numbers to non-blank lines by default. In http://pastebin.com/hF5yMMAz, it is clear that the if statements start on line 6 and line 12, not on the lines numbered 3 and 7 in your question. Use nl -ba to get the lines numbered correctly.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top