Блог пользователя buGMaster

Автор buGMaster, 10 лет назад, По-английски

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!

  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

»
10 лет назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

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

  • »
    »
    10 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    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!

    • »
      »
      »
      10 лет назад, # ^ |
      Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

      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

      • »
        »
        »
        »
        10 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        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?

  • »
    »
    10 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

  • »
    »
    10 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    How can I use the standalone version of this cleaner?

»
10 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
10 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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.

  • »
    »
    10 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    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?

    • »
      »
      »
      10 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      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.

  • »
    »
    10 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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

    • »
      »
      »
      10 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

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

      • »
        »
        »
        »
        10 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

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

        • »
          »
          »
          »
          »
          10 лет назад, # ^ |
          Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

          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

          • »
            »
            »
            »
            »
            »
            10 лет назад, # ^ |
              Проголосовать: нравится 0 Проголосовать: не нравится

            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?