Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How to find symbols in extern code files or projects and get their "type" (code analysis library)?
Forum Updated to NodeBB v4.3 + New Features

How to find symbols in extern code files or projects and get their "type" (code analysis library)?

Scheduled Pinned Locked Moved Unsolved C++ Gurus
c++ symbolscode analysislibrary
13 Posts 5 Posters 2.4k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F FeRDNYC
    2 Jun 2024, 21:01

    @Qtpp said in How to find symbols in extern code files or projects and get their "type" (code analysis library)?:

    I don't want to compile the code I'm going to analyse, if possible (is it?!)

    You probably want to at least come close to compiling the code, because... well, basically, because you have to, in order to determine what symbols the code actually produces.

    There are questions that have to be answered, like what header files will be loaded by #includes, what the actual value of any preprocessor symbols would actually be at compile time, what the result of various #if/#defined tests would therefore be, etc. Those are most easily answered by at least preprocessing the code, if not fully compiling it.

    (And also, if the code can't be compiled, that's probably a sign that it may not be worth trying to analyze either. Because the process of converting source code into symbols is exactly what a compiler does.)

    Most clang analysis tools require the path to a compile_commands.json for the files it's analyzing, so that it can pick up compile command lines, and know which directories to look in for header files and etc.

    Without knowing those things, you can't really interpret a given source file completely, because things like preprocessor conditionals create internal inconsistency if all branches are taken at once.

    Q Offline
    Q Offline
    Qtpp
    wrote on 3 Jun 2024, 12:42 last edited by Qtpp 6 Mar 2024, 13:35
    #3

    @FeRDNYC said in How to find symbols in extern code files or projects and get their "type" (code analysis library)?:

    You probably want to at least come close to compiling the code, because... well, basically, because you have to, in order to determine what symbols the code actually produces.

    Hey, thanks for your answer.
    Yea I thought so, which I definitely won't do.
    However my very first idea was to simply parse the code files, which also works to a certain extent only, since it's game over as soon as you face code like:

    auto obj = getObject();
    

    or

    test ( helloWorld->getHello()->getWorld() );
    

    Unfortunately only having the code files, you never gonna know what type of object auto obj is (returned by getObject) or what helloWorld->getHello()->getWorld() returns. Plus the defines or the other includes you mention.
    Might work for locals and member variables though.

    @FeRDNYC said in How to find symbols in extern code files or projects and get their "type" (code analysis library)?:

    (And also, if the code can't be compiled, that's probably a sign that it may not be worth trying to analyze either. Because the process of converting source code into symbols is exactly what a compiler does.)

    I thought of something that might be helpful for the whole Qt community. It should be possible to use it on individual files (that use Qt code) and entire projects, but... well... :(

    When I make my project a QtCreator plugin, so that it's not standalone but integrated into the "coding process".
    I have access to the symbols, clang/clazy and compile output, no?!
    THAT would actually be the best and easiest way, I guess.

    What do you think?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 3 Jun 2024, 19:41 last edited by
      #4

      Hi,

      Are you trying to reimplement something like the clang code model from Qt Creator ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Q 1 Reply Last reply 3 Jun 2024, 23:52
      0
      • S SGaist
        3 Jun 2024, 19:41

        Hi,

        Are you trying to reimplement something like the clang code model from Qt Creator ?

        Q Offline
        Q Offline
        Qtpp
        wrote on 3 Jun 2024, 23:52 last edited by
        #5

        @SGaist said in How to find symbols in extern code files or projects and get their "type" (code analysis library)?:

        Are you trying to reimplement something like the clang code model from Qt Creator ?

        Hi, not quite.
        At least this is not my intention just for the sake of doing it.

        I think I would be happy if I can access it from my QtCreator Plugin. Don't know if it's a good idea to go this way and if it helps to solve my issue. That's why I'm asking.

        Are you following the whole thread here?
        My only, but not so easy (as I found out and as @FeRDNYC told me) goal is it to extract variables (symbols) from code files or projects and do something with that information. I may not need all of them, and the user should decide which ones to include or exclude.
        To be 100% accurate, I am only interested in the particular classes and objects (i.e. their class names) that appear in a file.

        So, does a QtCreator Plugin have access to the "clang code model from QtCreator"?

        If so, how can I do it?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 6 Jun 2024, 06:29 last edited by
          #6

          From your description, it looks like you are after libclang to parse the code and extract information.

          I don't think the code model exposes stuff but you should check the sources to confirm.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          Q 1 Reply Last reply 7 Jun 2024, 18:46
          1
          • S SGaist
            6 Jun 2024, 06:29

            From your description, it looks like you are after libclang to parse the code and extract information.

            I don't think the code model exposes stuff but you should check the sources to confirm.

            Q Offline
            Q Offline
            Qtpp
            wrote on 7 Jun 2024, 18:46 last edited by
            #7

            @SGaist said in How to find symbols in extern code files or projects and get their "type" (code analysis library)?:

            From your description, it looks like you are after libclang to parse the code and extract information.

            That is looking promising.
            So I can use my own clang to parse and inspect the file(s)?!
            And the clang / code model used by the IDE is just read-only? You don't have access to it and can't gain access in some way by installing or modifying something?!

            But thanks, that helped. Will try libclang then... looks complicated to use tho :/

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 7 Jun 2024, 18:53 last edited by
              #8

              Libclang is just a library.

              As I wrote, I currently don't know how the code model is implemented and if you can tap in it. However, you can check that, the code of Qt Creator is available.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              Q 2 Replies Last reply 13 Jun 2024, 00:51
              0
              • S SGaist
                7 Jun 2024, 18:53

                Libclang is just a library.

                As I wrote, I currently don't know how the code model is implemented and if you can tap in it. However, you can check that, the code of Qt Creator is available.

                Q Offline
                Q Offline
                Qtpp
                wrote on 13 Jun 2024, 00:51 last edited by Qtpp
                #9

                @SGaist said in How to find symbols in extern code files or projects and get their "type" (code analysis library)?:

                As I wrote, I currently don't know how the code model is implemented and if you can tap in it. However, you can check that, the code of Qt Creator is available.

                Thanks for your reply.

                I've checked QtCreator on Github and was able to find the clangcodemodel plugin here. Meanwhile I realized that QtCreator itself is made out of many plugins for each purpose.

                Before I start to learn the whole thing for nothing, is it somehow possible to reach out to one of the QtCreator maintainers/devs, some person who knows all the internal QtCreator things well, so he/she can confirm or contradict that it's doable?!
                Because it feels like I can look at this code for month without finding out if it's worth to create a QtCreator plugin for my use case.
                A clear "yes" or "no, because...." would help a lot.

                PS:
                While scrolling through the GitHub folders I saw a plugin called "cppcheck" in there. Stumbled upon this "software" during my research on how to analyse and extract stuff from C++ code.
                Don't know exactly what the purpose of this is and if it's used by clang or maybe can be used in my case instead of clang?!
                Will continue to research :)

                jsulmJ 1 Reply Last reply 14 Jun 2024, 05:47
                0
                • Q Qtpp
                  13 Jun 2024, 00:51

                  @SGaist said in How to find symbols in extern code files or projects and get their "type" (code analysis library)?:

                  As I wrote, I currently don't know how the code model is implemented and if you can tap in it. However, you can check that, the code of Qt Creator is available.

                  Thanks for your reply.

                  I've checked QtCreator on Github and was able to find the clangcodemodel plugin here. Meanwhile I realized that QtCreator itself is made out of many plugins for each purpose.

                  Before I start to learn the whole thing for nothing, is it somehow possible to reach out to one of the QtCreator maintainers/devs, some person who knows all the internal QtCreator things well, so he/she can confirm or contradict that it's doable?!
                  Because it feels like I can look at this code for month without finding out if it's worth to create a QtCreator plugin for my use case.
                  A clear "yes" or "no, because...." would help a lot.

                  PS:
                  While scrolling through the GitHub folders I saw a plugin called "cppcheck" in there. Stumbled upon this "software" during my research on how to analyse and extract stuff from C++ code.
                  Don't know exactly what the purpose of this is and if it's used by clang or maybe can be used in my case instead of clang?!
                  Will continue to research :)

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 14 Jun 2024, 05:47 last edited by
                  #10

                  @Qtpp said in How to find symbols in extern code files or projects and get their "type" (code analysis library)?:

                  is it somehow possible to reach out to one of the QtCreator maintainers/devs

                  https://lists.qt-project.org/listinfo/qt-creator

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • S SGaist
                    7 Jun 2024, 18:53

                    Libclang is just a library.

                    As I wrote, I currently don't know how the code model is implemented and if you can tap in it. However, you can check that, the code of Qt Creator is available.

                    Q Offline
                    Q Offline
                    Qtpp
                    wrote on 28 Feb 2025, 17:31 last edited by
                    #11

                    @SGaist said in How to find symbols in extern code files or projects and get their "type" (code analysis library)?:

                    Libclang is just a library.

                    Made some progress in this case.
                    However most guides describe how to extend clang through your own custom written clang-tools (which requires re-compiling clang every time)...
                    I was thinking more about a standalone program which links (maybe statically) clang and accesses already existing API/tools like

                    • https://clang.llvm.org/docs/LibASTMatchersTutorial.html
                    • https://clang.llvm.org/docs/LibASTMatchers.html

                    This must also be possible right? Did not find anything like that as most guides show how to write clang "plugins"?!

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 28 Feb 2025, 20:01 last edited by
                      #12

                      cppcheck is a static analyzer however I don't know how it's currently implemented.

                      As for writing an application with libclang, I think their tutorial should get you started.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on 28 Feb 2025, 20:34 last edited by
                        #13

                        clazy is also using libclang

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved