Skip to content

Consider "main()" with typedef'd int return type as MainFunction #647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 26, 2024
Merged
2 changes: 2 additions & 0 deletions change_notes/2024-07-23-fix-fp-646-M0-1-10.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `M0-1-10` - `EncapsulatingFunctions.qll`:
- Fixes #646. Consider typedef'd `int` return types for `main()` function as MainFunction.
Empty file.
1 change: 1 addition & 0 deletions cpp/autosar/test/rules/M0-1-10.1/UnusedFunction.qlref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rules/M0-1-10/UnusedFunction.ql
34 changes: 34 additions & 0 deletions cpp/autosar/test/rules/M0-1-10.1/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <cstdint>

namespace mains {
static std::int32_t var;

// @brief namespace_func
static void
namespace_func(void) noexcept { // COMPLIANT: Called from "main" below.
mains::var = -1;
return;
}
} // namespace mains

std::int32_t func2() // COMPLIANT: Called from func1
{
return mains::var + 20;
}

std::int32_t func1() { // COMPLIANT: Called from main
return mains::var + func2(); // func2 called here.
}

// @brief main
// @return exit code
std::int32_t main(void) {
std::int32_t ret{0};
try {
ret = func1(); // func1 called here.
mains::var += ret;
} catch (...) {
mains::namespace_func(); // namespace_func called here.
}
return ret;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ abstract class EncapsulatingFunction extends Function { }
class MainFunction extends MainLikeFunction {
MainFunction() {
hasGlobalName("main") and
getType() instanceof IntType
getType().resolveTypedefs() instanceof IntType
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| test.cpp:5:9:5:12 | main |
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import cpp
import codingstandards.cpp.EncapsulatingFunctions

from MainFunction m
select m
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
typedef signed int int32_t;

// @brief main
// @return exit code
int32_t main(void) {
int32_t ret{0};
return ret;
}
Loading