Skip to content

Commit 164e064

Browse files
authored
fixed some -Wsuggest-attribute=returns_nonnull GCC warnings (#6950)
1 parent 1869eca commit 164e064

13 files changed

+20
-20
lines changed

cmake/compileroptions.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
6868
add_compile_options(-Wsuggest-attribute=noreturn)
6969
add_compile_options(-Wno-shadow) # whenever a local variable or type declaration shadows another one
7070
add_compile_options_safe(-Wuseless-cast)
71+
# add_compile_options_safe(-Wsuggest-attribute=returns_nonnull) # reports the warning even if the attribute is set
7172
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
7273
if(CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 14 OR CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 14)
7374
# TODO: verify this regression still exists in clang-15

lib/clangimport.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ namespace clangimport {
362362
void addFullScopeNameTokens(TokenList &tokenList, const Scope *recordScope);
363363
Scope *createScope(TokenList &tokenList, Scope::ScopeType scopeType, AstNodePtr astNode, const Token *def);
364364
Scope *createScope(TokenList &tokenList, Scope::ScopeType scopeType, const std::vector<AstNodePtr> &children2, const Token *def);
365-
Token *createTokensCall(TokenList &tokenList);
365+
RET_NONNULL Token *createTokensCall(TokenList &tokenList);
366366
void createTokensFunctionDecl(TokenList &tokenList);
367367
void createTokensForCXXRecord(TokenList &tokenList);
368368
Token *createTokensVarDecl(TokenList &tokenList);

lib/config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@
121121
# define DEPRECATED
122122
#endif
123123

124-
// TODO: GCC apparently also supports this but there is no documentation on it
125124
// returns_nonnull
126125
#if __has_cpp_attribute (gnu::returns_nonnull)
127126
# define RET_NONNULL [[gnu::returns_nonnull]]
128-
#elif (defined(__clang__) && ((__clang_major__ > 3) || ((__clang_major__ == 3) && (__clang_minor__ >= 7))))
127+
#elif (defined(__clang__) && ((__clang_major__ > 3) || ((__clang_major__ == 3) && (__clang_minor__ >= 7)))) \
128+
|| (defined(__GNUC__) && (__GNUC__ >= 9))
129129
# define RET_NONNULL __attribute__((returns_nonnull))
130130
#else
131131
# define RET_NONNULL

lib/cppcheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,8 +1103,8 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer)
11031103

11041104
if (mSettings.useSingleJob() || !mSettings.buildDir.empty()) {
11051105
// Analyse the tokens..
1106-
1107-
if (CTU::FileInfo * const fi1 = CTU::getFileInfo(tokenizer)) {
1106+
{
1107+
CTU::FileInfo * const fi1 = CTU::getFileInfo(tokenizer);
11081108
if (!mSettings.buildDir.empty())
11091109
mAnalyzerInformation.setFileInfo("ctu", fi1->toString());
11101110
if (mSettings.useSingleJob())

lib/cppcheck.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,15 @@ class CPPCHECKLIB CppCheck : ErrorLogger {
116116
* @brief Returns current version number as a string.
117117
* @return version, e.g. "1.38"
118118
*/
119-
static const char * version();
119+
RET_NONNULL static const char * version();
120120

121121
/**
122122
* @brief Returns extra version info as a string.
123123
* This is for returning extra version info, like Git commit id, build
124124
* time/date etc.
125125
* @return extra version info, e.g. "04d42151" (Git commit id).
126126
*/
127-
static const char * extraVersion();
127+
RET_NONNULL static const char * extraVersion();
128128

129129
/**
130130
* @brief Call all "getErrorMessages" in all registered Check classes.

lib/ctu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ namespace CTU {
144144
CPPCHECKLIB std::string getFunctionId(const Tokenizer &tokenizer, const Function *function);
145145

146146
/** @brief Parse current TU and extract file info */
147-
CPPCHECKLIB FileInfo *getFileInfo(const Tokenizer &tokenizer);
147+
CPPCHECKLIB RET_NONNULL FileInfo *getFileInfo(const Tokenizer &tokenizer);
148148

149149
CPPCHECKLIB std::list<FileInfo::UnsafeUsage> getUnsafeUsage(const Tokenizer &tokenizer, const Settings &settings, bool (*isUnsafeUsage)(const Settings &settings, const Token *argtok, MathLib::bigint *value));
150150

lib/symboldatabase.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ class CPPCHECKLIB Function {
10151015
void isInlineKeyword(bool state) {
10161016
setFlag(fIsInlineKeyword, state);
10171017
}
1018-
const Token *setFlags(const Token *tok1, const Scope *scope);
1018+
RET_NONNULL const Token *setFlags(const Token *tok1, const Scope *scope);
10191019
};
10201020

10211021
class CPPCHECKLIB Scope {
@@ -1440,7 +1440,7 @@ class CPPCHECKLIB SymbolDatabase {
14401440
void debugSymbolDatabase() const;
14411441

14421442
void addClassFunction(Scope *&scope, const Token *&tok, const Token *argStart);
1443-
static Function *addGlobalFunctionDecl(Scope*& scope, const Token* tok, const Token *argStart, const Token* funcStart);
1443+
RET_NONNULL static Function *addGlobalFunctionDecl(Scope*& scope, const Token* tok, const Token *argStart, const Token* funcStart);
14441444
Function *addGlobalFunction(Scope*& scope, const Token*& tok, const Token *argStart, const Token* funcStart);
14451445
void addNewFunction(Scope *&scope, const Token *&tok);
14461446
bool isFunction(const Token *tok, const Scope* outerScope, const Token *&funcStart, const Token *&argStart, const Token*& declEnd) const;

lib/templatesimplifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,7 @@ void TemplateSimplifier::simplifyTemplateAliases()
13461346
const Token * const fromStart = args[argnr].first;
13471347
const Token * const fromEnd = args[argnr].second->previous();
13481348
Token *temp = TokenList::copyTokens(tok1, fromStart, fromEnd, true);
1349-
const bool tempOK(temp && temp != tok1->next());
1349+
const bool tempOK(temp != tok1->next());
13501350
tok1->deleteThis();
13511351
if (tempOK)
13521352
tok1 = temp; // skip over inserted parameters

lib/tokenize.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2152,9 +2152,6 @@ void Tokenizer::simplifyTypedefCpp()
21522152

21532153
tok2 = TokenList::copyTokens(tok2, argStart, argEnd);
21542154
if (inTemplate) {
2155-
if (!tok2)
2156-
syntaxError(nullptr);
2157-
21582155
tok2 = tok2->next();
21592156
}
21602157

lib/tokenize.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class CPPCHECKLIB Tokenizer {
172172
* '; int *p(0);' => '; int *p = 0;'
173173
*/
174174
void simplifyInitVar();
175-
static Token* initVar(Token* tok);
175+
RET_NONNULL static Token* initVar(Token* tok);
176176

177177
/**
178178
* Simplify the location of "static" and "const" qualifiers in

lib/tokenlist.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class CPPCHECKLIB TokenList {
9494
* @param one_line true=>copy all tokens to the same line as dest. false=>copy all tokens to dest while keeping the 'line breaks'
9595
* @return new location of last token copied
9696
*/
97-
static Token *copyTokens(Token *dest, const Token *first, const Token *last, bool one_line = true);
97+
RET_NONNULL static Token *copyTokens(Token *dest, const Token *first, const Token *last, bool one_line = true);
9898

9999
/**
100100
* Create tokens from code.

lib/vfvalue.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,10 @@ namespace ValueFlow
327327

328328
enum class LifetimeScope : std::uint8_t { Local, Argument, SubFunction, ThisPointer, ThisValue } lifetimeScope = LifetimeScope::Local;
329329

330-
static const char* toString(MoveKind moveKind);
331-
static const char* toString(LifetimeKind lifetimeKind);
332-
static const char* toString(LifetimeScope lifetimeScope);
333-
static const char* toString(Bound bound);
330+
RET_NONNULL static const char* toString(MoveKind moveKind);
331+
RET_NONNULL static const char* toString(LifetimeKind lifetimeKind);
332+
RET_NONNULL static const char* toString(LifetimeScope lifetimeScope);
333+
RET_NONNULL static const char* toString(Bound bound);
334334

335335
/** How known is this value */
336336
enum class ValueKind : std::uint8_t {

lib/xml.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "config.h"
2323
#include "path.h"
2424

25+
SUPPRESS_WARNING_GCC_PUSH("-Wsuggest-attribute=returns_nonnull")
2526
SUPPRESS_WARNING_CLANG_PUSH("-Wzero-as-null-pointer-constant")
2627
SUPPRESS_WARNING_CLANG_PUSH("-Wsuggest-destructor-override")
2728
SUPPRESS_WARNING_CLANG_PUSH("-Winconsistent-missing-destructor-override")
@@ -33,6 +34,7 @@ SUPPRESS_WARNING_CLANG_POP
3334
SUPPRESS_WARNING_CLANG_POP
3435
SUPPRESS_WARNING_CLANG_POP
3536
SUPPRESS_WARNING_CLANG_POP
37+
SUPPRESS_WARNING_GCC_POP
3638

3739
inline static tinyxml2::XMLError xml_LoadFile(tinyxml2::XMLDocument& doc, const char* filename)
3840
{

0 commit comments

Comments
 (0)