Menu

Token is not recognized properly as function/typedef

2022-10-28
2022-11-08
  • Charles-the-intern

    Hello,
    I was getting the .dump file and notice a couple of problems that I hope you can help me with with a workaround:

    Token not recognized as function: I was parsing for function call and cppcheck doesn't recognize a few tokens as functions. I've included the required include paths with -I and even when outputting the .dump file, the function definition is in the symbol database as well (although the definition is in another header file). Is there a proper way to get this function, since it's in the symbol database?

    Token is a typedef but not simplified: cppcheck does the typedef simplification beforehand, but when I output the dump file, it wasn't simplified. And again, the typedef seems to be in the symbol database (under < typdef-info > at the end of the .dump file). Is there a proper way to get this typedef info via symbol database?

    Thank you very much for the help!

     

    Last edit: Charles-the-intern 2022-10-28
  • Daniel Marjamäki

    Typedefs are simplified in the dump file. I guess you are looking at the "rawtokens"? You shoulld look at the "tokenlist"

    code

    typedef int i32;
    i32 x;
    

    Dumpfile:

    <?xml version="1.0"?>
    <dumps>
      <platform name="Native" char_bit="8" short_bit="16" int_bit="32" long_bit="64" long_long_bit="64" pointer_bit="64"/>
      <rawtokens>
        <file index="0" name="1.c"/>
        <tok fileIndex="0" linenr="2" column="1" str="typedef"/>
        <tok fileIndex="0" linenr="2" column="9" str="int"/>
        <tok fileIndex="0" linenr="2" column="13" str="i32"/>
        <tok fileIndex="0" linenr="2" column="16" str=";"/>
        <tok fileIndex="0" linenr="4" column="1" str="i32"/>
        <tok fileIndex="0" linenr="4" column="5" str="x"/>
        <tok fileIndex="0" linenr="4" column="6" str=";"/>
      </rawtokens>
      <suppressions>
      </suppressions>
    <dump cfg="">
      <standards>
        <c version="c11"/>
        <cpp version="c++23"/>
      </standards>
      <directivelist>
      </directivelist>
      <tokenlist>
        <token id="0x55c6e5628c90" file="1.c" linenr="4" column="1" str="int" scope="0x55c6e56220b0" type="name"/>
        <token id="0x55c6e5629870" file="1.c" linenr="4" column="5" str="x" scope="0x55c6e56220b0" type="name" varId="1" exprId="1" variable="0x55c6e5616d50" valueType-type="int" valueType-sign="signed" valueType-reference="None"/>
        <token id="0x55c6e5628860" file="1.c" linenr="4" column="6" str=";" scope="0x55c6e56220b0"/>
      </tokenlist>
      <scopes>
        <scope id="0x55c6e56220b0" type="Global">
          <varlist>
            <var id="0x55c6e5616d50"/>
          </varlist>
        </scope>
      </scopes>
      <variables>
        <var id="0x55c6e5616d50" nameToken="0x55c6e5629870" typeStartToken="0x55c6e5628c90" typeEndToken="0x55c6e5628c90" access="Global" scope="0x55c6e56220b0" constness="0" isArray="false" isClass="false" isConst="false" isExtern="false" isPointer="false" isReference="false" isStatic="false" isVolatile="false"/>
      </variables>
      <valueflow>
      </valueflow>
      <typedef-info>
        <info name="i32" file="1.c" line="2" column="13" used="1"/>
      </typedef-info>
    </dump>
    </dumps>
    
     
    • Charles-the-intern

      @danielmarjamaki
      No, I'm looking at the tokenlist section... but for this problem seems like I found out the reason why:

      typedef fooooo foo
      inline foo f() {
          return foo();
      } 
      

      I notices, in the .dump file, that because function is inline, the return type (as part of function interface) is foo; even though inside the function definition it's still returning the original, actual type fooooo

      Does cppcheck deliberate do this?

       

      Last edit: Charles-the-intern 2022-10-31
      • Daniel Marjamäki

        there is a missing semicolon in your code example at the end of the first line.

         
        • Charles-the-intern

          sorry about the example... I meant to put the semicolon there as well.

          typedef fooooo foo;
          inline foo f() {
              return foo();
          } 
          

          dump file:

          <?xml version="1.0"?>
          <dumps>
            <platform name="win64" char_bit="8" short_bit="16" int_bit="32" long_bit="32" long_long_bit="64" pointer_bit="64"/>
            <rawtokens>
              <file index="0" name="main.cpp"/>
              <tok fileIndex="0" linenr="2" column="1" str="typedef"/>
              <tok fileIndex="0" linenr="2" column="9" str="fooooo"/>
              <tok fileIndex="0" linenr="2" column="16" str="foo"/>
              <tok fileIndex="0" linenr="2" column="19" str=";"/>
              <tok fileIndex="0" linenr="3" column="1" str="inline"/>
              <tok fileIndex="0" linenr="3" column="8" str="foo"/>
              <tok fileIndex="0" linenr="3" column="12" str="f"/>
              <tok fileIndex="0" linenr="3" column="13" str="("/>
              <tok fileIndex="0" linenr="3" column="14" str=")"/>
              <tok fileIndex="0" linenr="3" column="16" str="{"/>
              <tok fileIndex="0" linenr="4" column="5" str="return"/>
              <tok fileIndex="0" linenr="4" column="12" str="foo"/>
              <tok fileIndex="0" linenr="4" column="15" str="("/>
              <tok fileIndex="0" linenr="4" column="16" str=")"/>
              <tok fileIndex="0" linenr="4" column="17" str=";"/>
              <tok fileIndex="0" linenr="5" column="1" str="}"/>
            </rawtokens>
            <suppressions>
            </suppressions>
          <dump cfg="__cplusplus=201402L">
            <standards>
              <c version="c11"/>
              <cpp version="c++14"/>
            </standards>
            <directivelist>
            </directivelist>
            <tokenlist>
              <token id="00000214DC75B100" file="main.cpp" linenr="3" column="8" str="foo" scope="00000214DC885950" type="name"/>
              <token id="00000214DC75B580" file="main.cpp" linenr="3" column="12" str="f" scope="00000214DC885950" type="name" function="00000214DC760E10"/>
              <token id="00000214DC75C000" file="main.cpp" linenr="3" column="13" str="(" scope="00000214DC885950" link="00000214DC75B5E0"/>
              <token id="00000214DC75B5E0" file="main.cpp" linenr="3" column="14" str=")" scope="00000214DC885950" link="00000214DC75C000"/>
              <token id="00000214DC75B700" file="main.cpp" linenr="3" column="16" str="{" scope="00000214DC885310" link="00000214DC75B940"/>
              <token id="00000214DC75B760" file="main.cpp" linenr="4" column="5" str="return" scope="00000214DC885310" type="name" astOperand1="00000214DC75A860"/>
              <token id="00000214DC75ABC0" file="main.cpp" linenr="4" column="12" str="fooooo" scope="00000214DC885310" type="name" astParent="00000214DC75A860"/>
              <token id="00000214DC75A860" file="main.cpp" linenr="4" column="15" str="(" scope="00000214DC885310" link="00000214DC75B7C0" exprId="1" astParent="00000214DC75B760" astOperand1="00000214DC75ABC0"/>
              <token id="00000214DC75B7C0" file="main.cpp" linenr="4" column="16" str=")" scope="00000214DC885310" link="00000214DC75A860"/>
              <token id="00000214DC75BC40" file="main.cpp" linenr="4" column="17" str=";" scope="00000214DC885310"/>
              <token id="00000214DC75B940" file="main.cpp" linenr="5" column="1" str="}" scope="00000214DC885310" link="00000214DC75B700"/>
            </tokenlist>
            <scopes>
              <scope id="00000214DC885950" type="Global">
                <functionList>
                  <function id="00000214DC760E10" token="00000214DC75B580" tokenDef="00000214DC75B580" name="f" type="Function" access="Public" isInlineKeyword="true"/>
                </functionList>
              </scope>
              <scope id="00000214DC885310" type="Function" className="f" bodyStart="00000214DC75B700" bodyEnd="00000214DC75B940" nestedIn="00000214DC885950" function="00000214DC760E10"/>
            </scopes>
            <variables>
            </variables>
            <valueflow>
            </valueflow>
            <typedef-info>
              <info name="foo" file="main.cpp" line="2" column="16" used="1"/>
            </typedef-info>
          </dump>
          </dumps>
          

          if you're available, can you verify if this is a bug or cppcheck deliberately do this?

           
          • Daniel Marjamäki

            That is a bug. I have created this bug ticket: https://trac.cppcheck.net/ticket/11373

             
  • Charles-the-intern

    Regarding the typedef issue, a big reason why it wasn't working in my case is because of the setup where we have a few #includes inside namespace A {} AND extensive use of a precompiled header that specifies using namespace A for all translation units that include this precompiled header. The precompiled header, along with its recursively included headers, are all included correctly, but seems like these #includes being inside namespace causes the issue.

    ... perhaps this is also the reason why some tokens are not recognized as function properly

     

    Last edit: Charles-the-intern 2022-10-31
    • Daniel Marjamäki

      Maybe you have found some other bug. Can you create a small example code? It should not matter that a #include is inside a namespace the preprocessor does not care about that.

       
      • Charles-the-intern

        Please find below the attachments as an example. There're 2 issues but they're caused by the same reason: typdef is not simplified (different from above example) and token is not recognized as function.
        I noticed that if you don't do the using namespace and instead explicitly use the scope resolution ::, everything works.

        main.h

        namespace ns {
            #include "header.h"
        }
        

        header.h

        typedef int foo;
        
        foo function() {
            foo f = 0;
            return f;
        }
        

        main.cpp

        #include "main.h"
        
        using namespace ns;
        
        int main() {
            // ns::foo f = ns::function();
            foo f = function();
        
            return 0;
        }
        

        .dump

        <?xml version="1.0"?>
        <dumps>
          <platform name="win64" char_bit="8" short_bit="16" int_bit="32" long_bit="32" long_long_bit="64" pointer_bit="64"/>
          <rawtokens>
            <file index="0" name="main.cpp"/>
            <file index="1" name="main.h"/>
            <file index="2" name="header.h"/>
            <tok fileIndex="0" linenr="2" column="1" str="#"/>
            <tok fileIndex="0" linenr="2" column="2" str="include"/>
            <tok fileIndex="0" linenr="2" column="10" str="&quot;main.h&quot;"/>
            <tok fileIndex="0" linenr="4" column="1" str="using"/>
            <tok fileIndex="0" linenr="4" column="7" str="namespace"/>
            <tok fileIndex="0" linenr="4" column="17" str="ns"/>
            <tok fileIndex="0" linenr="4" column="19" str=";"/>
            <tok fileIndex="0" linenr="6" column="1" str="int"/>
            <tok fileIndex="0" linenr="6" column="5" str="main"/>
            <tok fileIndex="0" linenr="6" column="9" str="("/>
            <tok fileIndex="0" linenr="6" column="10" str=")"/>
            <tok fileIndex="0" linenr="6" column="12" str="{"/>
            <tok fileIndex="0" linenr="7" column="2" str="// ns::foo f = ns::function();"/>
            <tok fileIndex="0" linenr="8" column="2" str="foo"/>
            <tok fileIndex="0" linenr="8" column="6" str="f"/>
            <tok fileIndex="0" linenr="8" column="8" str="="/>
            <tok fileIndex="0" linenr="8" column="10" str="function"/>
            <tok fileIndex="0" linenr="8" column="18" str="("/>
            <tok fileIndex="0" linenr="8" column="19" str=")"/>
            <tok fileIndex="0" linenr="8" column="20" str=";"/>
            <tok fileIndex="0" linenr="10" column="2" str="return"/>
            <tok fileIndex="0" linenr="10" column="9" str="0"/>
            <tok fileIndex="0" linenr="10" column="10" str=";"/>
            <tok fileIndex="0" linenr="11" column="1" str="}"/>
          </rawtokens>
          <suppressions>
          </suppressions>
        <dump cfg="">
          <standards>
            <c version="c11"/>
            <cpp version="c++17"/>
          </standards>
          <directivelist>
            <directive file="main.cpp" linenr="2" str="#include &quot;main.h&quot;"/>
            <directive file="main.h" linenr="3" str="#include &quot;header.h&quot;"/>
          </directivelist>
          <tokenlist>
            <token id="000001C49EC851C0" file="main.h" linenr="2" column="1" str="namespace" scope="000001C49ECB4EA0" type="name"/>
            <token id="000001C49EC85640" file="main.h" linenr="2" column="11" str="ns" scope="000001C49ECB4EA0" type="name"/>
            <token id="000001C49EC856A0" file="main.h" linenr="2" column="14" str="{" scope="000001C49E961F40" link="000001C49E9202A0"/>
            <token id="000001C49E91F520" file="header.h" linenr="4" column="1" str="int" scope="000001C49E961F40" type="name"/>
            <token id="000001C49E91F640" file="header.h" linenr="4" column="5" str="function" scope="000001C49E961F40" type="name" function="000001C49E91B6E0"/>
            <token id="000001C49E91F040" file="header.h" linenr="4" column="13" str="(" scope="000001C49E961F40" link="000001C49E91F7C0" valueType-type="int" valueType-sign="signed" valueType-reference="None"/>
            <token id="000001C49E91F7C0" file="header.h" linenr="4" column="14" str=")" scope="000001C49E961F40" link="000001C49E91F040"/>
            <token id="000001C49E9201E0" file="header.h" linenr="4" column="16" str="{" scope="000001C49E962080" link="000001C49E91F1C0"/>
            <token id="000001C49E91EDA0" file="header.h" linenr="5" column="2" str="int" scope="000001C49E962080" type="name"/>
            <token id="000001C49E91F820" file="header.h" linenr="5" column="6" str="f" scope="000001C49E962080" type="name" varId="1" exprId="1" variable="000001C49EC7FEE0" valueType-type="int" valueType-sign="signed" valueType-reference="None"/>
            <token id="000001C49E91F0A0" file="header.h" linenr="5" column="8" str=";" scope="000001C49E962080" isSplittedVarDeclEq="true"/>
            <token id="000001C49E91F2E0" file="header.h" linenr="5" column="6" str="f" scope="000001C49E962080" type="name" varId="1" exprId="1" variable="000001C49EC7FEE0" astParent="000001C49E920240" valueType-type="int" valueType-sign="signed" valueType-reference="None"/>
            <token id="000001C49E920240" file="header.h" linenr="5" column="8" str="=" scope="000001C49E962080" type="op" isAssignmentOp="true" exprId="3" values="000001C49E91EA10" astOperand1="000001C49E91F2E0" astOperand2="000001C49E91F5E0" valueType-type="int" valueType-sign="signed" valueType-reference="None"/>
            <token id="000001C49E91F5E0" file="header.h" linenr="5" column="10" str="0" scope="000001C49E962080" type="number" isInt="true" values="000001C49E91E5B0" astParent="000001C49E920240" valueType-type="int" valueType-sign="signed" valueType-reference="None"/>
            <token id="000001C49E920120" file="header.h" linenr="5" column="11" str=";" scope="000001C49E962080"/>
            <token id="000001C49E91F100" file="header.h" linenr="6" column="2" str="return" scope="000001C49E962080" type="name" astOperand1="000001C49E920300" valueType-type="int" valueType-sign="signed" valueType-reference="None"/>
            <token id="000001C49E920300" file="header.h" linenr="6" column="9" str="f" scope="000001C49E962080" type="name" varId="1" exprId="1" variable="000001C49EC7FEE0" values="000001C49E91EAF0" astParent="000001C49E91F100" valueType-type="int" valueType-sign="signed" valueType-reference="None"/>
            <token id="000001C49E91FA00" file="header.h" linenr="6" column="10" str=";" scope="000001C49E962080"/>
            <token id="000001C49E91F1C0" file="header.h" linenr="7" column="1" str="}" scope="000001C49E962080" link="000001C49E9201E0"/>
            <token id="000001C49E9202A0" file="main.h" linenr="4" column="1" str="}" scope="000001C49E961F40" link="000001C49EC856A0"/>
            <token id="000001C49E91F940" file="main.cpp" linenr="4" column="1" str="using" scope="000001C49ECB4EA0" type="name"/>
            <token id="000001C49E91FA60" file="main.cpp" linenr="4" column="7" str="namespace" scope="000001C49ECB4EA0" type="name"/>
            <token id="000001C49E91EF80" file="main.cpp" linenr="4" column="17" str="ns" scope="000001C49ECB4EA0" type="name"/>
            <token id="000001C49E91FFA0" file="main.cpp" linenr="4" column="19" str=";" scope="000001C49ECB4EA0"/>
            <token id="000001C49E91FAC0" file="main.cpp" linenr="6" column="1" str="int" scope="000001C49ECB4EA0" type="name"/>
            <token id="000001C49E91FE20" file="main.cpp" linenr="6" column="5" str="main" scope="000001C49ECB4EA0" type="name" function="000001C49E91C540"/>
            <token id="000001C49E920360" file="main.cpp" linenr="6" column="9" str="(" scope="000001C49ECB4EA0" link="000001C49E91F400" valueType-type="int" valueType-sign="signed" valueType-reference="None"/>
            <token id="000001C49E91F400" file="main.cpp" linenr="6" column="10" str=")" scope="000001C49ECB4EA0" link="000001C49E920360"/>
            <token id="000001C49E91FB80" file="main.cpp" linenr="6" column="12" str="{" scope="000001C49E9621C0" link="000001C49E920420"/>
            <token id="000001C49E91F6A0" file="main.cpp" linenr="8" column="2" str="foo" scope="000001C49E9621C0" type="name"/>
            <token id="000001C49E91F580" file="main.cpp" linenr="8" column="6" str="f" scope="000001C49E9621C0" type="name" varId="2" exprId="2" variable="000001C49EC7F2A0"/>
            <token id="000001C49E91F280" file="main.cpp" linenr="8" column="8" str=";" scope="000001C49E9621C0" isSplittedVarDeclEq="true"/>
            <token id="000001C49E91FDC0" file="main.cpp" linenr="8" column="6" str="f" scope="000001C49E9621C0" type="name" varId="2" exprId="2" variable="000001C49EC7F2A0" astParent="000001C49E91ED40"/>
            <token id="000001C49E91ED40" file="main.cpp" linenr="8" column="8" str="=" scope="000001C49E9621C0" type="op" isAssignmentOp="true" exprId="4" astOperand1="000001C49E91FDC0" astOperand2="000001C49E91F700"/>
            <token id="000001C49E9200C0" file="main.cpp" linenr="8" column="10" str="function" scope="000001C49E9621C0" type="name" astParent="000001C49E91F700"/>
            <token id="000001C49E91F700" file="main.cpp" linenr="8" column="18" str="(" scope="000001C49E9621C0" link="000001C49E920180" exprId="5" astParent="000001C49E91ED40" astOperand1="000001C49E9200C0"/>
            <token id="000001C49E920180" file="main.cpp" linenr="8" column="19" str=")" scope="000001C49E9621C0" link="000001C49E91F700"/>
            <token id="000001C49E91F880" file="main.cpp" linenr="8" column="20" str=";" scope="000001C49E9621C0"/>
            <token id="000001C49E91F8E0" file="main.cpp" linenr="10" column="2" str="return" scope="000001C49E9621C0" type="name" astOperand1="000001C49E91FEE0" valueType-type="int" valueType-sign="signed" valueType-reference="None"/>
            <token id="000001C49E91FEE0" file="main.cpp" linenr="10" column="9" str="0" scope="000001C49E9621C0" type="number" isInt="true" values="000001C49E91EA30" astParent="000001C49E91F8E0" valueType-type="int" valueType-sign="signed" valueType-reference="None"/>
            <token id="000001C49E9203C0" file="main.cpp" linenr="10" column="10" str=";" scope="000001C49E9621C0"/>
            <token id="000001C49E920420" file="main.cpp" linenr="11" column="1" str="}" scope="000001C49E9621C0" link="000001C49E91FB80"/>
          </tokenlist>
          <scopes>
            <scope id="000001C49ECB4EA0" type="Global">
              <functionList>
                <function id="000001C49E91C540" token="000001C49E91FE20" tokenDef="000001C49E91FE20" name="main" type="Function" access="Public"/>
              </functionList>
            </scope>
            <scope id="000001C49E961F40" type="Namespace" className="ns" bodyStart="000001C49EC856A0" bodyEnd="000001C49E9202A0" nestedIn="000001C49ECB4EA0">
              <functionList>
                <function id="000001C49E91B6E0" token="000001C49E91F640" tokenDef="000001C49E91F640" name="function" type="Function" access="Public"/>
              </functionList>
            </scope>
            <scope id="000001C49E962080" type="Function" className="function" bodyStart="000001C49E9201E0" bodyEnd="000001C49E91F1C0" nestedIn="000001C49E961F40" function="000001C49E91B6E0">
              <varlist>
                <var id="000001C49EC7FEE0"/>
              </varlist>
            </scope>
            <scope id="000001C49E9621C0" type="Function" className="main" bodyStart="000001C49E91FB80" bodyEnd="000001C49E920420" nestedIn="000001C49ECB4EA0" function="000001C49E91C540">
              <varlist>
                <var id="000001C49EC7F2A0"/>
              </varlist>
            </scope>
          </scopes>
          <variables>
            <var id="000001C49EC7F2A0" nameToken="000001C49E91F580" typeStartToken="000001C49E91F6A0" typeEndToken="000001C49E91F6A0" access="Local" scope="000001C49E9621C0" constness="0" isArray="false" isClass="true" isConst="false" isExtern="false" isPointer="false" isReference="false" isStatic="false" isVolatile="false"/>
            <var id="000001C49EC7FEE0" nameToken="000001C49E91F820" typeStartToken="000001C49E91EDA0" typeEndToken="000001C49E91EDA0" access="Local" scope="000001C49E962080" constness="0" isArray="false" isClass="false" isConst="false" isExtern="false" isPointer="false" isReference="false" isStatic="false" isVolatile="false"/>
          </variables>
          <valueflow>
            <values id="000001C49E91EA10">
              <value intvalue="0" bound="Point" known="true" path="0"/>
            </values>
            <values id="000001C49E91E5B0">
              <value intvalue="0" bound="Point" known="true" path="0"/>
            </values>
            <values id="000001C49E91EAF0">
              <value intvalue="0" bound="Point" known="true" path="0"/>
            </values>
            <values id="000001C49E91EA30">
              <value intvalue="0" bound="Point" known="true" path="0"/>
            </values>
          </valueflow>
          <typedef-info>
            <info name="foo" file="header.h" line="2" column="13" used="1"/>
          </typedef-info>
        </dump>
        </dumps>
        

        Thank you very much for logging these tickets! I know these issues probably won't get fixed before next release, still I look forward to the next version!

         

        Last edit: Charles-the-intern 2022-11-08
        • Charles-the-intern

          @danielmarjamaki
          sorry to bother you, but I was wondering if you can verify with the above example that it's a bug?

          Thanks very much!

           

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.