How to configure CppCheck to discourage usage of a function












1















Reading CppCheck's List of Checks and the checkfunctions.h I noticed the feature:




Warn if a function is called whose usage is discouraged




I did not understand how to configure this, though. More specifically I want




  1. One of my own implemented functions to be discouraged

  2. A 3rdparty function (e.g. OpenCV's cv::imwrite()) to be discouraged. I am linking the pre-builts of this library so it would be hard (but not impossible) to change the source code to achieve it


How can I annotate these functions or how can I add them to CppCheck's list of "functions non grata"?










share|improve this question



























    1















    Reading CppCheck's List of Checks and the checkfunctions.h I noticed the feature:




    Warn if a function is called whose usage is discouraged




    I did not understand how to configure this, though. More specifically I want




    1. One of my own implemented functions to be discouraged

    2. A 3rdparty function (e.g. OpenCV's cv::imwrite()) to be discouraged. I am linking the pre-builts of this library so it would be hard (but not impossible) to change the source code to achieve it


    How can I annotate these functions or how can I add them to CppCheck's list of "functions non grata"?










    share|improve this question

























      1












      1








      1








      Reading CppCheck's List of Checks and the checkfunctions.h I noticed the feature:




      Warn if a function is called whose usage is discouraged




      I did not understand how to configure this, though. More specifically I want




      1. One of my own implemented functions to be discouraged

      2. A 3rdparty function (e.g. OpenCV's cv::imwrite()) to be discouraged. I am linking the pre-builts of this library so it would be hard (but not impossible) to change the source code to achieve it


      How can I annotate these functions or how can I add them to CppCheck's list of "functions non grata"?










      share|improve this question














      Reading CppCheck's List of Checks and the checkfunctions.h I noticed the feature:




      Warn if a function is called whose usage is discouraged




      I did not understand how to configure this, though. More specifically I want




      1. One of my own implemented functions to be discouraged

      2. A 3rdparty function (e.g. OpenCV's cv::imwrite()) to be discouraged. I am linking the pre-builts of this library so it would be hard (but not impossible) to change the source code to achieve it


      How can I annotate these functions or how can I add them to CppCheck's list of "functions non grata"?







      c++ c++11 visual-c++ static-analysis cppcheck






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 8:34









      PhilLabPhilLab

      2,6181247




      2,6181247
























          2 Answers
          2






          active

          oldest

          votes


















          3














          The check uses configuration. Nothing is hardcoded. Write a custom cfg file and use --library to load that.



          You can write the cfg file manually, it is xml format. Or you can use the GUI (it is not the best GUI ever but imho it works).



          If you have a function foo that is deprecated then you will write something like:



          <function name="foo">
          <warn severity="style" alternatives="bar" reason="Deprecated"/>
          <arg nr="1"/>
          </function>


          You can specify a custom warning message also:



          <function name="foo">
          <warn severity="warning">Do not use foo(). Use bar() instead.</warn>
          <arg nr="1"/>
          </function>


          For each argument that the function takes you need to provide a <arg>.



          Let me know if you have problems.






          share|improve this answer



















          • 1





            if you create a simple opencv.cfg then feel free to upstream it. I would be happy to include it in cppcheck so everybody that uses opencv will get warned about its deprecated functions.

            – Daniel Marjamäki
            Nov 20 '18 at 6:06











          • Not to raise confusion: cv::imwrite() is not widely discouraged at all. It just so happens that we have our own implementation which we prefer in our specific application only.

            – PhilLab
            Dec 20 '18 at 14:43



















          3














          I am not certain if there is a generic way to check the usage of all the third party functions. Probably @Daniel Marjamäki is the best person to answer this. But did you try creating a rule for it?



          If you wish to check for the exact signature of the function, you could have something like this:



          <?xml version="1.0"?>
          <rule version="1">
          <pattern>cv::imwrite()</pattern>
          <message>
          <id>discouragedFunction</id>
          <summary>The use of the function cv::imwrite is discouraged.</summary>
          </message>
          </rule>


          Or if you want something more on the generic side, you could have something like this:



          <?xml version="1.0"?>
          <rule version="1">
          <pattern>cv::[_a-zA-Z][_a-zA-Z0-9]+()</pattern>
          <message>
          <id>discouragedFunction</id>
          <summary>The use of the function opencv functions are discouraged.</summary>
          </message>
          </rule>





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295937%2fhow-to-configure-cppcheck-to-discourage-usage-of-a-function%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            The check uses configuration. Nothing is hardcoded. Write a custom cfg file and use --library to load that.



            You can write the cfg file manually, it is xml format. Or you can use the GUI (it is not the best GUI ever but imho it works).



            If you have a function foo that is deprecated then you will write something like:



            <function name="foo">
            <warn severity="style" alternatives="bar" reason="Deprecated"/>
            <arg nr="1"/>
            </function>


            You can specify a custom warning message also:



            <function name="foo">
            <warn severity="warning">Do not use foo(). Use bar() instead.</warn>
            <arg nr="1"/>
            </function>


            For each argument that the function takes you need to provide a <arg>.



            Let me know if you have problems.






            share|improve this answer



















            • 1





              if you create a simple opencv.cfg then feel free to upstream it. I would be happy to include it in cppcheck so everybody that uses opencv will get warned about its deprecated functions.

              – Daniel Marjamäki
              Nov 20 '18 at 6:06











            • Not to raise confusion: cv::imwrite() is not widely discouraged at all. It just so happens that we have our own implementation which we prefer in our specific application only.

              – PhilLab
              Dec 20 '18 at 14:43
















            3














            The check uses configuration. Nothing is hardcoded. Write a custom cfg file and use --library to load that.



            You can write the cfg file manually, it is xml format. Or you can use the GUI (it is not the best GUI ever but imho it works).



            If you have a function foo that is deprecated then you will write something like:



            <function name="foo">
            <warn severity="style" alternatives="bar" reason="Deprecated"/>
            <arg nr="1"/>
            </function>


            You can specify a custom warning message also:



            <function name="foo">
            <warn severity="warning">Do not use foo(). Use bar() instead.</warn>
            <arg nr="1"/>
            </function>


            For each argument that the function takes you need to provide a <arg>.



            Let me know if you have problems.






            share|improve this answer



















            • 1





              if you create a simple opencv.cfg then feel free to upstream it. I would be happy to include it in cppcheck so everybody that uses opencv will get warned about its deprecated functions.

              – Daniel Marjamäki
              Nov 20 '18 at 6:06











            • Not to raise confusion: cv::imwrite() is not widely discouraged at all. It just so happens that we have our own implementation which we prefer in our specific application only.

              – PhilLab
              Dec 20 '18 at 14:43














            3












            3








            3







            The check uses configuration. Nothing is hardcoded. Write a custom cfg file and use --library to load that.



            You can write the cfg file manually, it is xml format. Or you can use the GUI (it is not the best GUI ever but imho it works).



            If you have a function foo that is deprecated then you will write something like:



            <function name="foo">
            <warn severity="style" alternatives="bar" reason="Deprecated"/>
            <arg nr="1"/>
            </function>


            You can specify a custom warning message also:



            <function name="foo">
            <warn severity="warning">Do not use foo(). Use bar() instead.</warn>
            <arg nr="1"/>
            </function>


            For each argument that the function takes you need to provide a <arg>.



            Let me know if you have problems.






            share|improve this answer













            The check uses configuration. Nothing is hardcoded. Write a custom cfg file and use --library to load that.



            You can write the cfg file manually, it is xml format. Or you can use the GUI (it is not the best GUI ever but imho it works).



            If you have a function foo that is deprecated then you will write something like:



            <function name="foo">
            <warn severity="style" alternatives="bar" reason="Deprecated"/>
            <arg nr="1"/>
            </function>


            You can specify a custom warning message also:



            <function name="foo">
            <warn severity="warning">Do not use foo(). Use bar() instead.</warn>
            <arg nr="1"/>
            </function>


            For each argument that the function takes you need to provide a <arg>.



            Let me know if you have problems.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 20 '18 at 6:02









            Daniel MarjamäkiDaniel Marjamäki

            2,164710




            2,164710








            • 1





              if you create a simple opencv.cfg then feel free to upstream it. I would be happy to include it in cppcheck so everybody that uses opencv will get warned about its deprecated functions.

              – Daniel Marjamäki
              Nov 20 '18 at 6:06











            • Not to raise confusion: cv::imwrite() is not widely discouraged at all. It just so happens that we have our own implementation which we prefer in our specific application only.

              – PhilLab
              Dec 20 '18 at 14:43














            • 1





              if you create a simple opencv.cfg then feel free to upstream it. I would be happy to include it in cppcheck so everybody that uses opencv will get warned about its deprecated functions.

              – Daniel Marjamäki
              Nov 20 '18 at 6:06











            • Not to raise confusion: cv::imwrite() is not widely discouraged at all. It just so happens that we have our own implementation which we prefer in our specific application only.

              – PhilLab
              Dec 20 '18 at 14:43








            1




            1





            if you create a simple opencv.cfg then feel free to upstream it. I would be happy to include it in cppcheck so everybody that uses opencv will get warned about its deprecated functions.

            – Daniel Marjamäki
            Nov 20 '18 at 6:06





            if you create a simple opencv.cfg then feel free to upstream it. I would be happy to include it in cppcheck so everybody that uses opencv will get warned about its deprecated functions.

            – Daniel Marjamäki
            Nov 20 '18 at 6:06













            Not to raise confusion: cv::imwrite() is not widely discouraged at all. It just so happens that we have our own implementation which we prefer in our specific application only.

            – PhilLab
            Dec 20 '18 at 14:43





            Not to raise confusion: cv::imwrite() is not widely discouraged at all. It just so happens that we have our own implementation which we prefer in our specific application only.

            – PhilLab
            Dec 20 '18 at 14:43













            3














            I am not certain if there is a generic way to check the usage of all the third party functions. Probably @Daniel Marjamäki is the best person to answer this. But did you try creating a rule for it?



            If you wish to check for the exact signature of the function, you could have something like this:



            <?xml version="1.0"?>
            <rule version="1">
            <pattern>cv::imwrite()</pattern>
            <message>
            <id>discouragedFunction</id>
            <summary>The use of the function cv::imwrite is discouraged.</summary>
            </message>
            </rule>


            Or if you want something more on the generic side, you could have something like this:



            <?xml version="1.0"?>
            <rule version="1">
            <pattern>cv::[_a-zA-Z][_a-zA-Z0-9]+()</pattern>
            <message>
            <id>discouragedFunction</id>
            <summary>The use of the function opencv functions are discouraged.</summary>
            </message>
            </rule>





            share|improve this answer




























              3














              I am not certain if there is a generic way to check the usage of all the third party functions. Probably @Daniel Marjamäki is the best person to answer this. But did you try creating a rule for it?



              If you wish to check for the exact signature of the function, you could have something like this:



              <?xml version="1.0"?>
              <rule version="1">
              <pattern>cv::imwrite()</pattern>
              <message>
              <id>discouragedFunction</id>
              <summary>The use of the function cv::imwrite is discouraged.</summary>
              </message>
              </rule>


              Or if you want something more on the generic side, you could have something like this:



              <?xml version="1.0"?>
              <rule version="1">
              <pattern>cv::[_a-zA-Z][_a-zA-Z0-9]+()</pattern>
              <message>
              <id>discouragedFunction</id>
              <summary>The use of the function opencv functions are discouraged.</summary>
              </message>
              </rule>





              share|improve this answer


























                3












                3








                3







                I am not certain if there is a generic way to check the usage of all the third party functions. Probably @Daniel Marjamäki is the best person to answer this. But did you try creating a rule for it?



                If you wish to check for the exact signature of the function, you could have something like this:



                <?xml version="1.0"?>
                <rule version="1">
                <pattern>cv::imwrite()</pattern>
                <message>
                <id>discouragedFunction</id>
                <summary>The use of the function cv::imwrite is discouraged.</summary>
                </message>
                </rule>


                Or if you want something more on the generic side, you could have something like this:



                <?xml version="1.0"?>
                <rule version="1">
                <pattern>cv::[_a-zA-Z][_a-zA-Z0-9]+()</pattern>
                <message>
                <id>discouragedFunction</id>
                <summary>The use of the function opencv functions are discouraged.</summary>
                </message>
                </rule>





                share|improve this answer













                I am not certain if there is a generic way to check the usage of all the third party functions. Probably @Daniel Marjamäki is the best person to answer this. But did you try creating a rule for it?



                If you wish to check for the exact signature of the function, you could have something like this:



                <?xml version="1.0"?>
                <rule version="1">
                <pattern>cv::imwrite()</pattern>
                <message>
                <id>discouragedFunction</id>
                <summary>The use of the function cv::imwrite is discouraged.</summary>
                </message>
                </rule>


                Or if you want something more on the generic side, you could have something like this:



                <?xml version="1.0"?>
                <rule version="1">
                <pattern>cv::[_a-zA-Z][_a-zA-Z0-9]+()</pattern>
                <message>
                <id>discouragedFunction</id>
                <summary>The use of the function opencv functions are discouraged.</summary>
                </message>
                </rule>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 10:12









                OptimatonOptimaton

                3815




                3815






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295937%2fhow-to-configure-cppcheck-to-discourage-usage-of-a-function%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Bressuire

                    Vorschmack

                    Quarantine