0

I wanted to find whether the cpp file has a certain function implemented.

For example if a() is present in the cpp file. How do I write a rule for this? I want it throw exception, if it is not present.

Alex
  • 195
  • 7

3 Answers3

1

static code analysis tools are not used to find the compile or linking errors.

If the 'a` function or method is not declared and defined the compiler or linker will inform you - you will get an error.

0___________
  • 60,014
  • 4
  • 34
  • 74
  • No, I wanted to enforce some cpp files implement a function (ex: a()). Can we specify rules for cppcheck, to see if the function name really exist in the cpp file. – Kaushik Ramachandran Dec 18 '18 at 10:23
  • 1
    compiler and the linker will enforce it. You will not get the executable file and you will be forced to implement those functions. – 0___________ Dec 18 '18 at 10:25
  • the program will not link sucessfully if `a` is not defined anywhere, maybe OP wants to make sure the function is defined in one specific source file – 463035818_is_not_an_ai Dec 18 '18 at 10:29
  • No. let me elaborate the use-case. I have a function a(). I need only a subset of cpp files to declare and define a(). If this is not implemented by the developer (both in .cpp and .h), I want this to be caught automatically. – Kaushik Ramachandran Dec 18 '18 at 10:30
  • If it is not defined and implemented - and it is used somewhere in the code you will not compile/link. If it is not used - it is not needed. Static code analysis tools are used to warn programmers if they find some suspicious language constructions - which may affect the program execution. But at the first place the porogram **has to** compile and link – 0___________ Dec 18 '18 at 10:43
  • I wouldn't use cppcheck to enforce something like this - wrong tool for the job, as others have said. I'd create a separate build target (e.g. in a makefile) that only depends on two source files - one that contains a `main()` that declares and calls `a()`, without defining it, and the other that is required to define it. The build of that target will then only succeed if the required source file implements that function. Repeat as needed for all such functions and the files that are required to implement them. Then create a dummy build target which depends on all the others .... – Peter Dec 18 '18 at 11:33
1

If the CppCheck can't detect this kind of check, you can try CppDepend and its code query language CQLinq to create your custom rules.

from file in Files where file.ChildMethods.Where(m=>m.Name=="a").Count()==0 select file
0

I am a Cppcheck developer.

Cppcheck has "rules" and "addons". A "rule" is executed by "--rule". With a "rule" you cannot check this as far as I see.

With an "addon" you can definitely implement this. A good start is to loop through the scope list. If you see a function scope for "a()" you can see where the scope body is.

You can read more about addons in the cppcheck manual. http://cppcheck.sourceforge.net/manual.pdf

Chapter 12.

Daniel Marjamäki
  • 2,907
  • 15
  • 16