Commit Graph

8874 Commits

Author SHA1 Message Date
Daniel Marjamäki 5087f15035 Travis: Try to fix 'make checkcfg' 2019-03-02 19:38:11 +01:00
Daniel Marjamäki 8a3b73ffdb Do not write extra uninitvar warnings 2019-03-02 13:17:15 +01:00
Rikard Falkeborn 40ead7fd25 Add help to testrunner (#1704)
For now, only print the ways of running testrunner and the few options
that are available.

Also, refactor to remove an unneeded const_cast and use a range for loop.

Partially fixes #8514.
2019-03-02 08:06:23 +01:00
orbitcowboy a5ac9fcac5 Running astyle [ci skip]. 2019-03-01 15:05:53 +01:00
Daniel Marjamäki 845c407ab7 Fixed pattern matching in isVariableChanged() to handle normal token list better 2019-03-01 13:23:03 +01:00
orbitcowboy 360823203f windows.cfg: Ensure the return value is taken into account for memory (re)-allocation functions. 2019-03-01 08:58:53 +01:00
IOBYTE b78b3c6ab1 Fixed #9005 (Syntax error on valid C++) (#1716) 2019-03-01 08:18:53 +01:00
Armin Müller f4b5b156d7 Typos found by running "codespell" (#1715) 2019-03-01 01:01:39 +01:00
Daniel Marjamäki 5563fef7bb Fixed #9008 (new crash in clang test suite) 2019-02-28 20:34:07 +01:00
Daniel Marjamäki 4f5a426fe5 Improved isVariableChangedByFunctionCall, better logic when parameter might be passed by reference 2019-02-28 17:02:46 +01:00
Daniel Marjamäki 494dd2ba3a Revert "Improved isVariableChangedByFunctionCall, better logic when parameter might be passed by reference"
This reverts commit 14a0031e88.
2019-02-28 15:56:25 +01:00
versat f6c3749015 gtk.cfg: Add support for g_alloca() and g_newa().
References:
https://developer.gnome.org/glib/stable/glib-Memory-Allocation.html#g-alloca
https://github.com/GNOME/glib/blob/master/glib/galloca.h
2019-02-28 15:29:37 +01:00
Daniel Marjamäki 14a0031e88 Improved isVariableChangedByFunctionCall, better logic when parameter might be passed by reference 2019-02-28 10:26:47 +01:00
Daniel Marjamäki 857681a049 Make quick fix for uninitialized variable false positive. Will look more at this soon. 2019-02-28 09:52:52 +01:00
IOBYTE 98bf112352 template simplifier: fix recursive variable templates (#1711) 2019-02-28 08:30:04 +01:00
Daniel Marjamäki e27a7a585f Fix uninitvar false positive when taking address of variable 2019-02-27 18:44:30 +01:00
Daniel Marjamäki 80143725dd Fixed #8999 (False positive uninitvar related to casting) 2019-02-27 17:58:25 +01:00
Martin Ettl fe402498e1 qt.cfg: Added support for Q_DECLARE_PUBLIC()-macro. 2019-02-27 15:38:31 +01:00
Martin Ettl 17c1733b0e gnu.cfg: Added support for asprintf() and vasprintf(). There is a FN with memory leak detection regarding pointer args, which is already mentioned in #8980. A comment has been added. 2019-02-27 15:17:34 +01:00
Daniel Marjamäki 226f0c7544 Fixed ValueType for auto variable 2019-02-27 10:28:18 +01:00
Paul Fultz II 032020c40d Fix issue 9001: FP: Found suspicious operator ',' [constStatement] 2019-02-27 07:09:22 +01:00
IOBYTE 9d75b718d3 template simplifier: remove use of simplifyTokenList2 in tests (#1705) 2019-02-27 07:06:34 +01:00
Paul Fultz II e846312fed Check subtraction of pointers to different objects 2019-02-27 06:55:48 +01:00
rikardfalkeborn 0e988cc755 Fix #8992: Add originalTypeToken to auto (#1701) 2019-02-27 06:44:31 +01:00
Daniel Marjamäki c44e2ed378 sizeof: write inconclusive warning if calculation in sizeof is done indirectly by macro 2019-02-26 21:06:44 +01:00
Daniel Marjamäki b248075aae Comment bailout
[ci skip]
2019-02-26 19:28:11 +01:00
Daniel Marjamäki 9e93e89a4d UninitVar: Fix false positives when there is possible cast 2019-02-26 19:26:46 +01:00
Kamil Dudka 21c22d0d4d TestNullPointer: add regression test to cover #8813 (#1699)
... which is fixed since 1.86-72-gbc34f0239
2019-02-26 17:14:45 +01:00
Sebastian 61f911d39a
qt.cfg: Add Q_DECLARE_TR_FUNCTIONS() and others (#1677)
Add unknownMacro Q_DECLARE_TR_FUNCTIONS found via daca@home.
Add function QCoreApplication::translate() and macro
QT_DECLARE_DEPRECATED_TR_FUNCTIONS() the unknownMacro depends on.
Reference:
https://doc.qt.io/qt-5/qcoreapplication.html#Q_DECLARE_TR_FUNCTIONS
https://doc.qt.io/qt-5/qcoreapplication.html#translate
2019-02-26 13:39:37 +01:00
IOBYTE bf85767829 template simplifier: make sure all instantiations are found and expan… (#1696)
* template simplifier: make sure all instantiations are found and expanded in #5097

* template simplifier: check output on another test

* template simplifier: add output to another test
2019-02-26 06:41:04 +01:00
IOBYTE 3f257d6310 template simplifier: instantiate template class when something inside… (#1695)
* template simplifier: instantiate template class when something inside class instantiated.

* template simplifier: add output to another test that now works
2019-02-25 21:01:34 +01:00
Daniel Marjamäki d84d8885f7 SymbolDatabase: Fix TODO assertions 2019-02-24 19:11:18 +01:00
IOBYTE 41d87d6306 template simplifier: check output of a few crash and hang checks that now generate correct output (#1689) 2019-02-24 10:31:49 +01:00
Daniel Marjamäki ef731064bd Fixed #8964 (ValueType: auto constness) 2019-02-24 08:16:08 +01:00
Daniel Marjamäki c855cf8682 ValueType: Better constness for auto variable 2019-02-23 18:16:18 +01:00
Daniel Marjamäki b43572d75e Fixed #8969 (C++ functional cast not handled correctly) 2019-02-23 17:29:53 +01:00
Daniel Marjamäki 68062e3702 Fixed #8954 (false positive: Local variable x shadows outer variable) 2019-02-23 16:22:22 +01:00
Paul Fultz II fd3c1fd040 Fix issue 1777: Undefined Behavior: Comparing pointers to different objects
This uses the lifetime analysis to check when comparing pointer that point to different objects:

```cpp
int main(void)
{
    int foo[10];
    int bar[10];
    int diff;

    if(foo > bar)   // Undefined Behavior
    {
       diff = 1;
    }

    return 0;
}
```
2019-02-23 08:32:08 +01:00
amai2012 7859d7d879 #3030 add another regression test 2019-02-22 21:10:05 +01:00
Paul Fultz II 941dd79f0d Fix FP issue 8986: constArgument check warns for elements of const array (#1683) 2019-02-22 06:39:59 +01:00
Paul Fultz II 507c7a4388 Improvement to lifetime tracking of addressof and derefencing
This will now warn for cases like this:

```cpp
auto& f() {
    std::vector<int> x;
    return x[0];
}
```

It also improves the handling of address of operator, so it can now warn across some function calls, like this:

```cpp
int& f(int& a) {
    return a;
}
int* hello() {
    int x = 0;
    return &f(x);
}
```
2019-02-22 06:38:56 +01:00
Paul Fultz II 715714f4de Forward lifetimes in "for" loops (#1682)
* Forward lifetimes in for loops

* Format
2019-02-22 06:37:02 +01:00
Paul Fultz II 0ee3f678b5 Fix issue 8987: False positive knownConditionTrueFalse (#1678) 2019-02-20 15:28:31 +01:00
orbitcowboy c3244cb359 Fixed regression introduced by my previous commit. 2019-02-20 11:11:20 +01:00
Paul Fultz II dfaf75db54 Fix issue 8984: FP constStatement regression with qualified name (#1676) 2019-02-19 07:08:36 +01:00
versat dc63cb9f98 gtk.cfg: Add more details for g_print() and g_printerr()
Reference:
https://developer.gnome.org/glib/stable/glib-Warnings-and-Assertions.html#g-print
2019-02-18 16:48:46 +01:00
Kamil Dudka 2908593cf6 checkautovariables: eliminate false positives on assignment of &ptr->item (#1667)
Even if `ptr` is a local variable, the object `ptr->item` might be not.
So taking address of `ptr->item` is definitely not unsafe in general.

This commit fixes false positives triggered by commit
1.85-249-gf42648fe2 on the following code of sssd:

https://github.com/SSSD/sssd/blob/d409df33/src/sbus/request/sbus_request.c#L359
2019-02-18 09:35:07 +01:00
Paul Fultz II cf1ad5087a Extend constStatement checker
This reworks constStatement to find more issues. It catches issue [8827](https://trac.cppcheck.net/ticket/8827):

```cpp
extern void foo(int,const char*,int);
void f(int value)
{
        foo(42,"test",42),(value&42);
}
```

It also catches from issue [8451](https://trac.cppcheck.net/ticket/8451):

```cpp
void f1(int x) {
    1;
    (1);
    (char)1;
    ((char)1);
    !x;
    (!x);
    ~x;
}
```

And also:

```cpp
void f(int x) {
    x;
}
```

The other examples are not caught due to incomplete AST.
2019-02-15 13:31:40 +01:00
rikardfalkeborn dc4e7cef88 Run simplifyPlatformTypes on library return types (#1672)
Add a call to simplifyPlatformTypes() in
SymbolDatabase::setValueTypeInTokenList() to simplify return types of
library configured functions. This fixes the FN in #8141. Regression
tests are added, both for the original issue and another FN in the comments.

In order to do that, move simplifyPlatformTypes() to TokenList from Tokenizer.
This is a pure refactoring and does not change any behaviour. The code was
literally copy-pasted from one file to another and in two places
'list.front()' was changed to 'front()'.

When adding the call to simplifyPlatformTypes(), the original type of
v.size() where v is a container is changed from 'size_t' to 'std::size_t'.
Tests are updated accordingly. It can be noted that if v is declared as
'class fred : public std::vector<int> {} v', the original type of 'v.size()'
is still 'size_t' and not 'std::size_t'.
2019-02-15 13:29:52 +01:00
versat 4dae640e8c gtk.cfg: Add type conversion macros
Reference:
https://developer.gnome.org/glib/stable/glib-Type-Conversion-Macros.html
daca@home found that they are missing.
2019-02-15 08:44:21 +01:00
IOBYTE 8bd5b3eccf Fixed #8962 ("(debug) Unknown type 'T'" with template typename parame… (#1671)
* Fixed #8962 ("(debug) Unknown type 'T'" with template typename parameter)

Only simple one parameter template functions with one function parameter
are supported.

* Added TODO test case for FIXME.
2019-02-14 11:48:59 +01:00
versat ed25ae3acb gtk.cfg: Add some configurations often reported as missing by daca@home
Add two g_assert*(), g_signal_connect_data(), some g_signal_connect*() functions and macros and g_strdup_printf().
2019-02-13 15:35:46 +01:00
versat fe6ea282d3 qt.cfg: Add support for QT_TR_NOOP* and QT_TRANSLATE_NOOP* macros
Reference:
https://doc.qt.io/qt-5/i18n-source-translation.html#using-qt-tr-noop-and-qt-translate-noop-in-c
2019-02-12 10:21:56 +01:00
IOBYTE 9dc5dbe1ab Fixed #8976 (Crash on `using mystring = std::string;`) (#1663)
* Fixed #8976 (Crash on `using mystring = std::string;`)

* templateSimplifier: move test case to correct file.
2019-02-12 09:39:01 +01:00
versat 8207e48adc qt.cfg: Add support for macros Q_LIKELY and Q_UNLIKELY
Reference:
https://doc.qt.io/qt-5/qtglobal.html#Q_LIKELY
https://doc.qt.io/qt-5/qtglobal.html#Q_UNLIKELY
2019-02-12 08:21:49 +01:00
Sebastian 2c1f97d787
qt.cfg: Add support for macros `forever` and `Q_FOREVER` (#1662)
Reference:
https://doc.qt.io/qt-5/qtglobal.html#forever
https://doc.qt.io/qt-5/qtglobal.html#Q_FOREVER
2019-02-11 16:35:01 +01:00
Sebastian 3427e61383
std.cfg: Add support for std::string::assign() (#1660)
Reference: https://en.cppreference.com/w/cpp/string/basic_string/assign
2019-02-11 13:40:36 +01:00
IOBYTE 9490d0db8c token simplifier: fix crashes related to #8972 (#1659) 2019-02-11 07:45:03 +01:00
Daniel Marjamäki 6ca1aba4a7 UninitVar: Fix --experimental-fast issues 2019-02-10 19:00:01 +01:00
Simon Martin eaaff30e65 Ticket #8878: Properly simplify typedefs within template instantiations. (#1656) 2019-02-10 09:45:33 +01:00
IOBYTE 155e4ce912 Fixed #8971 ("(debug) Unknown type 'x'." using alias in class members) (#1653)
* Fixed #8971 ("(debug) Unknown type 'x'." using alias in class members)

* template simplifier: partial fix for #8972

Add support for multi-token default template parameters.

* template simplifier: fix for #8971

Remove typename outside of templates.
2019-02-09 08:34:59 +01:00
Daniel Marjamäki bd7790fd8c Update copyright year 2019-02-09 07:24:06 +01:00
Paul Fultz II d7c20b15e7 Fix FP with const argument when doing a variable and cast (#1649) 2019-02-07 14:59:09 +01:00
Sebastian 5fe7aad5e3
qt.cfg: Add configuration and tests for macro Q_NULLPTR (#1651)
Qt defines `Q_NULLPTR` with `nullptr` if it is available, otherwise with `NULL`.
Since there seems to be no (sane) way to configure it the same way in the library configuration it is just defined with `NULL`.
2019-02-07 12:27:25 +01:00
IOBYTE 7025254c26 Fixed #8969 (syntax error: template) (#1647)
Fixed template detection to handle multi-token template parameters.
2019-02-07 08:50:49 +01:00
rikardfalkeborn aa730f45c6 Add regression test for #7714 (#1648)
Ticket #7714 was fixed in ea215c3b7b.
Add a regression test to make sure it stays fixed.
2019-02-07 08:49:55 +01:00
Sebastian 55ce6d2073
qt.cfg: Add support and tests for QFile::exists function (#1645)
Found by daca@home
2019-02-06 13:23:05 +01:00
Daniel Marjamäki 14a312e310 useInitializationList: Skip warning when rhs is a multi line lambda. For readability it might be better to have assignments in constructor in this case. 2019-02-05 19:53:10 +01:00
IOBYTE 1faae52d06 Fixed #8960 ("(debug) Unknown type 'x'." with alias in template class alias) (#1643)
* Fixed #8960 ("(debug) Unknown type 'x'." with alias in template class alias)

This commit adds non-template type alias support to the template
simplifier.  Only relatively simple type aliases are supported at this
time. More complex types will be added later.

--debug-warnings will show unsupported type aliases.

Type alias support will be removed from the symbol database in the
future.  Type alias tests have been removed from the symbol database
tests.

* Add the changes.

* Fix codacy warning.

* Fix travis warnings.
2019-02-05 08:52:23 +01:00
Daniel Marjamäki a4406aca32 Fixed #7845 (Leak reported when ignoring return value of 'new', even if pointer saved by constructor) 2019-02-03 12:15:05 +01:00
Daniel Marjamäki ae001d4336 Fixed #8957 (Tokenizer::setVarId: varid not set when lambda function is used) 2019-02-03 08:57:04 +01:00
Daniel Marjamäki 4457faa26b Fixed #8850 (Array in-class initialization for private member considered uninitialized) 2019-02-02 18:34:41 +01:00
Daniel Marjamäki 9d8f798aca Fixed #8951 (false postive: (style) The class 'x' does not have a constructor although it has private member variables.) 2019-02-02 18:25:26 +01:00
Daniel Marjamäki a878f90299 Fix Cppcheck warning about unused variable 2019-02-01 07:01:08 +01:00
IOBYTE d08aa666f6 template simplifier: fix crash on windows (#1639)
* template simplifier: fix crash on windows

Use right token when searching for template type alias to delete.

* template simplifier: fix a cppcheck warning
2019-02-01 06:59:49 +01:00
IOBYTE 98fc6d1d32 Fixed #8959 ("(debug) Unknown type 'x'" with using/alias) (#1635)
* Fixed #8959 ("(debug) Unknown type 'x'" with using/alias)

* fix cppcheck warning
2019-01-31 23:57:37 +01:00
rikardfalkeborn 91de606243 TestOther: Shorter test cases (#1637)
* Remove newlines after check(
* Remove unneeded statements after if-statements

As an example, the previous test case

	check(
	    "bool foo(int x) {\n"
	    "  if (x < 0)"
	    "    return true;\n"
	    "  return false;\n"
	    "}");

is changed to

	check("void foo(int x) {\n"
	      "  if (x < 0) {}\n"
	      "}");
2019-01-31 23:57:05 +01:00
Daniel Marjamäki 961f66baff Fixed #8820 (import GUI project)
This has basic handling of GUI projects. But further work will be needed to handle addons etc, the plan is that we will be able to run addons from the command line soon.
2019-01-31 20:40:21 +01:00
IOBYTE 05a3e6807b Fixed #8950 and #8952 (improve type alias support) (#1633)
* Fixed #8950 and #8952 (improve type alias support)

* fix travis build
2019-01-31 16:53:51 +01:00
Paul Fultz II c176775afb Avoid infinite recursion in getLifetimeVariable (#1634)
* Fix direct recursion

* Limit depth of getLifetimeVariable
2019-01-31 10:34:41 +01:00
rikardfalkeborn 7779a9186e Use valueflow in unsigned less than zero checker (#1630)
The unsigned less than zero checker looked for patterns like "<= 0".
Switching to use valueflow improves the checker in a few aspects.

First, it removes false positives where instead of 0, the code is using
0L, 0U, etc. Instead of having to hard code the different variants of 0,
valueflow handles this automatically. This fixes FPs on the form

	uint32_t value = 0xFUL;
	void f() {
  		if (value < 0u)
		{
			value = 0u;
		}
	}

where 0u was previously not recognized by the checker. This fixes #8836.

Morover, it makes it possible to handle templates properly. In commit
fa076598ad, all warnings inside templates
were made inconclusive, since the checker had no idea if "0" came from
a template parameter or not.

This makes it possible to not warn for the following case which was
reported as a FP in #3233

	template<int n> void foo(unsigned int x) {
	if (x <= n);
	}
	foo<0>();

but give a warning for the following case

	template<int n> void foo(unsigned int x) {
	if (x <= 0);
	}

Previously, both these cases gave inconclusive warnings.

Finally, it makes it possible to give warnings for the following code:

	void f(unsigned x) {
		int y = 0;
		if (x <= y) {}
	}

Also, previously, the checker for unsigned variables larger than 0, the
checker used the string of the astoperand. This meant that for code like
the following:

	void f(unsigned x, unsigned y) {
		if (x -y >= 0) {}
	}

cppcheck would output

	[unsigned-expression-positive.c] (style) Unsigned variable '-' can't be negative so it is unnecessary to test it.

using expressionString() instead gives a better error message

        [unsigned-expression-positive.c] (style) Unsigned expression 'x-z' can't be negative so it is unnecessary to test it.
2019-01-31 09:30:29 +01:00
Paul Fultz II 165a22ed0f Lifetime: Support analysis with functions that do not return a reference (#1632)
* Initial support for function return

* Add test case

* Add support for reference parameters

* Format
2019-01-29 09:47:52 +01:00
versat 62b9368b3c gtk.cfg: Add configurations for g_free() and g_malloc() / g_malloc0()
Reference: https://developer.gnome.org/glib/stable/glib-Memory-Allocation.html
daca@home found these missing function configurations among others.
Also add tests for g_malloc() and g_free().
2019-01-28 13:47:46 +01:00
Daniel Marjamäki 54cebfaf94 Fixed #8392 (false positive: "Memory leak" with malloc in if) 2019-01-27 10:27:44 +01:00
Daniel Marjamäki 2bba9ac78a Fixed #8948 (False Positive: Variable 'n' is assigned a value that is never used.) 2019-01-26 21:44:07 +01:00
Paul Fultz II d6aaf401df Lifetime: Follow functions that return references
This will now warn for cases like this:

```cpp
int& f(int& a) {
    return a;
}
int& hello() {
    int x = 0;
    return f(x);
}
```
2019-01-26 11:03:57 +01:00
IOBYTE 68bbe15116 template simplifier: fix missing instantiation (#1627) 2019-01-26 07:08:54 +01:00
Sebastian b1f68229f2
Library cfg tests: Enable all tests again, exclude regressions for now (#1626)
Use `--check-library` for all tests as it was done before.
Re-enable all tests in runtests.sh again.
The regressions where runtests.sh would fail are disabled via "FIXME"
comment in the inline suppression comment.
2019-01-25 17:03:16 +01:00
Sebastian ce96ffd433
gtk.cfg: Add internationalization macros / functions (#1623)
Found via Donate CPU / daca@home.
Reference: https://developer.gnome.org/glib/stable/glib-I18N.html
Update header comment in gtk.cfg since it is now only edited manually.
2019-01-25 13:07:48 +01:00
rikardfalkeborn a05079fef7 Add regression test for #8780 (#1619)
Ticket #8780 was fixed in b839ad60dd.
Add a test to avoid regressions.
2019-01-25 07:48:18 +01:00
IOBYTE cb1a1df0fa template simplifier: fix out of line member function scope and use more full name matching (#1617) 2019-01-24 07:21:22 +01:00
rikardfalkeborn 080c9d53af 8205 regression test (#1618)
* Fix trac ticket reference

* Add regression test for #8205

Ticket #8205 was fixed in f65cf220ba.
Add a test case to avoid regressions.
2019-01-24 07:20:26 +01:00
Daniel Marjamäki c773e6ed10 runtests.sh: uncomment the xmlstarlet checking 2019-01-23 09:40:43 +01:00
rikardfalkeborn 8356ec6774 Add regression test for #6906 and #7284 (#1614)
* Add regression test for #6906

Ticket #6906 was fixed in f65cf220ba.
Add a test to make sure there are no regressions.

* Add regression test for #7284

Ticket #7284 was fixed in 5d1fdf7958.
Add tests to avoid regressions.
2019-01-23 09:09:03 +01:00
IOBYTE ec8bc785a2 template simplifier: add support for using namespace when instantiating templates (#1615) 2019-01-23 08:53:01 +01:00
Paul Fultz II 3975913637 Extend lifetime checking for references
This will use the lifetime checker for dangling references. It will find these cases for indirectly assigned reference:

```cpp
int &foo()
{
    int s = 0;
    int& x = s;
    return x;
}
```

This will also fix issue 510 as well:

```cpp
int &f( int k )
{
    static int &r = k;
    return r;
}
```
2019-01-23 07:29:16 +01:00
Daniel Marjamäki 8c07be136a Fixed #8949 (False Positive: Variable 'f' is assigned a value that is never used.) 2019-01-22 21:16:27 +01:00
Daniel Marjamäki 87489c3ffd Try to make Travis happy 2019-01-22 18:56:12 +01:00
Daniel Marjamäki f03ce97aae unmatched suppression: fix test case 2019-01-22 18:47:52 +01:00
versat 6f62b83fe6 checkcfg: Add regression test for umatchedSuppression errors
As discussed in https://trac.cppcheck.net/ticket/8931 a regression test is added
to the test/cfg/runtests.sh script to make sure that unmatchedSuppression messages result in an Cppcheck exit code that signals a failure.
2019-01-21 20:53:13 +01:00