buGMaster's blog

By buGMaster, 9 years ago, In English

Hi again...
I need a tool which parses a source code (especially a c++ code) and generates a new one, that's all the unused parts of the original source code (such as includes, macros, functions, variables, ...) is removed in it.
I think, this tool helps in reducing compile time and run time; also makes the code more cleaner which makes debugging easier.
Is there such a tool? If no, how can I produce this tool? (I mean, how can I recognize unused parts of a my code?)
Any help would be appreciated!

  • Vote: I like it
  • +3
  • Vote: I do not like it

»
9 years ago, # |
  Vote: I like it +5 Vote: I do not like it

Well, I try to do that in my plugin using CLion API, but sometimes it deletes more than needed because CLion doesn't find references correctly.

See removeUnusedCode and DeletionMarkingVisitor

Maybe you could do something similar or start here and workaround broken search

  • »
    »
    9 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    I don't use CLion. I plan to test it with your plugin (JHelper), maybe later!
    Could you please explain the idea behind it? how does it work? I don't know anything about Java!

    • »
      »
      »
      9 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      Well, the idea is someone (CLion devs) parse c++ and represent source as tree of abstract objects like "function call" or "class declaration" and also provides a way to find where some object used. My code is a pretty straightforward usage of that API.

      I don't think one should need java to read that code, it's pretty clear if you used any cstyle language

      • »
        »
        »
        »
        9 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Great! what about I don't have this tree?! How can I generate it?
        Is there any other solution for this except making a tree of abstractions?!
        I think, this functionality can be find in compiler. What's your idea?
        Do you know any other tools?

  • »
    »
    9 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    How can I test/use this API and your functionality outside of the CLion?

    • »
      »
      »
      9 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      You may try to decompile their code and check what they do:)

      • »
        »
        »
        »
        9 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        I think, it's harder than making a new API. Do you agree?! What about any other solution?

  • »
    »
    9 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    How can I use the standalone version of this cleaner?

»
9 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I've found ahmed_aly add a unused code cleaner for KawigiEdit (a Topcoder plugin).
Anybody knows this feature and the way it works?

»
9 years ago, # |
  Vote: I like it 0 Vote: I do not like it

My caide tool removes (some of) unused C++ code too :). You can test it like this:

mkdir dir
cd dir
caide init
caide problem test
<Put your code into test/main.cpp file and clean test/test.cpp file>
caide make

The file submission.cpp will be created with unused code removed.

I can make a standalone code cleaner if you like it.

  • »
    »
    9 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    Thanks for your reply.
    Could you please explain how does it work? Which strategy you used to recognize unused codes?
    How can I use this code cleaner standalone?

    • »
      »
      »
      9 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Could you please explain how does it work?

      It uses clang API to parse the AST and detect code unreachable from main().

      How can I use this code cleaner standalone?

      You cannot yet. I will need to build another executable for that.

  • »
    »
    9 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    I've checked here for clean, but I don't get anything. Could you please explain?

    • »
      »
      »
      9 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Ahem, by "cleaning" test/test.cpp I meant replacing it with an empty file :)

      • »
        »
        »
        »
        9 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        :D Aha...
        I try to make the standalone version of cleaner.

        • »
          »
          »
          »
          »
          9 years ago, # ^ |
          Rev. 2   Vote: I like it 0 Vote: I do not like it

          https://github.com/slycelote/caide/releases/tag/cppinliner-0.1

          • In Windows with Visual Studio installed: cppcleaner.exe -target i386-pc-windows-msvc -- optimize main.cpp >output.cpp
          • In 64bit Linux: cppcleaner -target x86_64-linux -- optimize main.cpp >output.cpp
          • In 32bit Linux: cppcleaner -target i686-linux -- optimize main.cpp >output.cpp

          You may need to add paths to system include directories like this: -isystem /full/path (before the -- separator). In Linux you can see which directories are required with g++ -v -x c++ -E /dev/null

          • »
            »
            »
            »
            »
            »
            9 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            Thanks for the standalone version! I couldn't use it. I've downloaded this for linux. After the extraction, it gives me an executable file with name "cppcleaner-linux".
            How can I clean a code with it?