GithubHelp home page GithubHelp logo

Comments (2)

llvmbot avatar llvmbot commented on August 16, 2024

@llvm/issue-subscribers-clang-frontend

Author: Anatol Pomozov (anatol)

Hello, I started using clang's `-ast-dump` feature and I found that the AST it produces does not contain all original information.

It is easier to see it with the following example:

int func1();

typedef double (*callback)(int param1);

The test.c above contains 1 function and one callback declaration (a typedef for a function pointer type). Now let's produce the JSON AST for the example above with clang -Xclang -ast-dump=json test.c. We get the AST and the the bits relevant to our example are

The func1 AST:

{
      "id": "0x598de3e85e50",
      "kind": "FunctionDecl",
      "loc": {
        "offset": 4,
        "file": "llvm.c",
        "line": 1,
        "col": 5,
        "tokLen": 5
      },
      "range": {
        "begin": {
          "offset": 0,
          "col": 1,
          "tokLen": 3
        },
        "end": {
          "offset": 10,
          "col": 11,
          "tokLen": 1
        }
      },
      "name": "func1",
      "mangledName": "func1",
      "type": {
        "qualType": "int ()"
      }
    },

In this example there is no node for the return type (int). We have a full function signature that includes the function parameters. But it makes sense to have a node for the return type itself to avoid parsing types at the client side. Note that callback declaration below does contain a node for the return type.

Now let's look at the second example for callback:

    {
      "id": "0x598de3e860b8",
      "kind": "TypedefDecl",
      "loc": {
        "offset": 31,
        "line": 3,
        "col": 18,
        "tokLen": 8
      },
      "range": {
        "begin": {
          "offset": 14,
          "col": 1,
          "tokLen": 7
        },
        "end": {
          "offset": 51,
          "col": 38,
          "tokLen": 1
        }
      },
      "name": "callback",
      "type": {
        "qualType": "double (*)(int)"
      },
      "inner": [
        {
          "id": "0x598de3e86050",
          "kind": "PointerType",
          "type": {
            "qualType": "double (*)(int)"
          },
          "inner": [
            {
              "id": "0x598de3e85ff0",
              "kind": "ParenType",
              "type": {
                "qualType": "double (int)"
              },
              "inner": [
                {
                  "id": "0x598de3e85fc0",
                  "kind": "FunctionProtoType",
                  "type": {
                    "qualType": "double (int)"
                  },
                  "cc": "cdecl",
                  "inner": [
                    {
                      "id": "0x598de3e270e0",
                      "kind": "BuiltinType",
                      "type": {
                        "qualType": "double"
                      }
                    },
                    {
                      "id": "0x598de3e26fc0",
                      "kind": "BuiltinType",
                      "type": {
                        "qualType": "int"
                      }
                    }
                  ]
                }
              ]
            }
          ]
        }

In this example the function parameters (e.g. node 0x598de3e26fc0) do not contain parameter names. Note that function declaration does contain such information.

$ clang --version
clang version 17.0.6
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

from llvm-project.

shafik avatar shafik commented on August 16, 2024

CC @AaronBallman

from llvm-project.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.