Menu

AST broken, ternary operator missing operand(s) [internalAstError]

Mads Brix
2020-05-13
2020-10-07
  • Mads Brix

    Mads Brix - 2020-05-13

    Running cppcheck 2.0 on lines of code like this:
    {"B", (state == true) ? "UNDEF" : "DEF"};

    The output is
    AST broken, ternary operator missing operand(s) [internalAstError]

    Changing to
    {"B", state == true ? "UNDEF" : "DEF"};
    the error is not there.

    It does not happen in cppcheck 1.90

     
  • Daniel Marjamäki

    can you show how I can reproduce?

    I get no such error for this code:

    A b[] = {"B", (state == true) ? "UNDEF" : "DEF"};
    
     
  • Mads Brix

    Mads Brix - 2020-05-13

    This give the error for me:
    int main()
    {
    {"B", (state == true) ? "UNDEF" : "DEF"};
    }
    I know that it does not compile, but e.g. adding 'A b[] =' in front hide the error.

    I run cppcheck without any commandline options.

    Btw, I compiled cppcheck without Z3, since that did not compile in my environment (I think there is another discussion about that).

     
    • Daniel Marjamäki

      hmm somehow it would feel good with a bit more reasonable syntax. Missing type/variable declarations is fine, I don't worry about that. Is it possible for you to bisect your code and try to keep a reasonable syntax?

      just so we do not make a fix for your example code and that will not help anybody in reality.

       

      Last edit: Daniel Marjamäki 2020-05-19
      • Mads Brix

        Mads Brix - 2020-05-20

        Absolutely fair request.
        This example compiles and still exposed the error.

        #include <string>
        #include <vector>
        
        void 
        DoIt(const std::string& name,
             std::vector<std::pair<std::string, std::string>>&& item)
        {
        }
        
        int main()
        {
           bool abc = true;
        
           DoIt("HH", {
                   {"T", "123"},
                   {"ID", "456"},
                   {"State", "789"},
                   {"B", (abc) ? "UNDEF" : "DEF"}
                });
        
           return 0;
        }
        
         
        • Daniel Marjamäki

           
  • Zufu Liu

    Zufu Liu - 2020-05-18

    There is another AST broken: 'if' doesn't have two operands.

    https://github.com/zufuliu/notepad2/blob/master/scintilla/src/PerLine.cxx#L78

    bool MarkerHandleSet::RemoveNumber(int markerNum, bool all) {
        bool performedDeletion = false;
        mhList.remove_if([&](const MarkerHandleNumber &mhn) noexcept {
            if ((all || !performedDeletion) && (mhn.number == markerNum)) {
                performedDeletion = true;
                return true;
            }
            return false;
        });
        return performedDeletion;
    }
    
     
  • Daniel Marjamäki

    Thanks! I created this trac ticket: https://trac.cppcheck.net/ticket/9729

     
  • Mads Brix

    Mads Brix - 2020-10-07

    Hi,
    not sure if it is correct to reuse this one, but seem to be related.
    When I run cppcheck 2.2 on the code snip below I get
    AST broken, ternary operator missing operand(s) [internalAstError]

    Not there with cppcheck 2.1.

    #include <string>
    #include <vector>
    
    class A
    {
       public:
          A(const std::string& x, const std::vector<std::string>& y) {}
    };
    
    void 
    DoIt(const A& a)
    {
    }
    
    int main()
    {
       bool abc = true;
    
       DoIt({"X",
            {"Y", (abc == false ? "C" : "D")}
           });
    
       return 0;
    }
    
     

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.