Menu

the result is wrong while importing boost library

顾涛明
2018-04-12
2018-04-18
  • 顾涛明

    顾涛明 - 2018-04-12
    #include <iostream>
    #include <vector>
    #include <boost/algorithm/string.hpp>
    using namespace std;
    using namespace boost;
    
    int main() {
    
        int ss[3];
        ss[4]=0;  //out of bounds
    
        string str("readme.txt");
        if (ends_with(str, " txt")) {
            cout << to_upper_copy(str) + "UPPER" << endl;              // upper case
        }
    
        replace_first(str, "readme ", "followme ");                       // replace
        cout << str << endl;
    
        vector<char> v(str.begin(), str.end());
        vector<char> v2 = to_upper_copy(erase_first_copy(v, "txt ")); // delete sub string
        for (int i = 0; i < v2.size(); ++i) {
            cout << v2[i];
        }
    
        return 0;
    }
    

    For the above code, the following command can detect the error:

    $ cppcheck --force --enable=all  -I /usr/include/ --output-file=cppcheck.txt main.cpp
    
    [main.cpp:10]: (error) Array 'ss[3]' accessed at index 4, which is out of bounds.
    [main.cpp:10]: (style) Variable 'ss' is assigned a value that is never used.
    (information) Cppcheck cannot find all the include files (use --check-config for details)
    

    But if I import the boost_1_66_0, the result will misrecognize many issues about boost, and cannot detect the real error in main.cpp. Does anybody can solve this problem?

    $ cppcheck --force --enable=all -I /usr/local/include/ -I /usr/include/ --output-file=cppcheck.txt main.cpp
    
    [/usr/local/include/boost/preprocessor/config/config.hpp:92]: (error) #elif without #if
    [/usr/local/include/boost/config.hpp:39]: (error) No header in #include
    [/usr/local/include/boost/algorithm/string/std/slist_traits.hpp:16]: (error) No header in #include
    [/usr/local/include/boost/preprocessor/variadic/elem.hpp:92]: (error) #endif without #if
    [/usr/local/include/boost/preprocessor/config/config.hpp:82]: (error) failed to expand         'BOOST_PP_VARIADICS_MSVC', Invalid ## usage when expanding 'BOOST_PP_VARIADICS_MSVC'.
    [/usr/local/include/boost/config.hpp:57]: (error) No header in #include
    [/usr/local/include/boost/config.hpp:48]: (error) No header in #include
    [/usr/local/include/boost/config.hpp:30]: (error) No header in #include
    (information) Cppcheck cannot find all the include files (use --check-config for details)
    
     

    Last edit: 顾涛明 2018-04-12
  • versat

    versat - 2018-04-12

    I can reproduce this.
    While Cppcheck is not really meant to work with system/library include files (better use library configurations for that), i think there is also a problem with parsing the boost header file.
    This could maybe really be an issue relevant to Cppcheck.
    For code like this (similar to boosts config.hpp):

    #
    # if 1
    #  define a 1
    # endif
    
    int main()
    {
        printf("%d", a);
    }
    

    simplecpp (part of Cppcheck) reports this error:

    $ ./simplecpp include.hpp
    
    include.hpp:4: syntax error: #endif without #if
    

    Removing the single # at the top makes it work again.

     

    Last edit: versat 2018-04-12
  • versat

    versat - 2018-04-12
     
    • 顾涛明

      顾涛明 - 2018-04-12

      Thank you very much!

       
  • versat

    versat - 2018-04-18

    A short update:
    The issue #125 has been fixed in simplecpp and it already works better parsing the boost headers, but there is still an issue.
    I created issue #126 for simplecpp which addresses the new problem.

     

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.