Skip to content

Commit

Permalink
fixed some -Wsuggest-attribute=returns_nonnull GCC warnings (#6950)
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave authored Nov 29, 2024
1 parent 1869eca commit 164e064
Show file tree
Hide file tree
Showing 13 changed files with 20 additions and 20 deletions.
1 change: 1 addition & 0 deletions cmake/compileroptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_compile_options(-Wsuggest-attribute=noreturn)
add_compile_options(-Wno-shadow) # whenever a local variable or type declaration shadows another one
add_compile_options_safe(-Wuseless-cast)
# add_compile_options_safe(-Wsuggest-attribute=returns_nonnull) # reports the warning even if the attribute is set
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
if(CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 14 OR CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 14)
# TODO: verify this regression still exists in clang-15
Expand Down
2 changes: 1 addition & 1 deletion lib/clangimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ namespace clangimport {
void addFullScopeNameTokens(TokenList &tokenList, const Scope *recordScope);
Scope *createScope(TokenList &tokenList, Scope::ScopeType scopeType, AstNodePtr astNode, const Token *def);
Scope *createScope(TokenList &tokenList, Scope::ScopeType scopeType, const std::vector<AstNodePtr> &children2, const Token *def);
Token *createTokensCall(TokenList &tokenList);
RET_NONNULL Token *createTokensCall(TokenList &tokenList);
void createTokensFunctionDecl(TokenList &tokenList);
void createTokensForCXXRecord(TokenList &tokenList);
Token *createTokensVarDecl(TokenList &tokenList);
Expand Down
4 changes: 2 additions & 2 deletions lib/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@
# define DEPRECATED
#endif

// TODO: GCC apparently also supports this but there is no documentation on it
// returns_nonnull
#if __has_cpp_attribute (gnu::returns_nonnull)
# define RET_NONNULL [[gnu::returns_nonnull]]
#elif (defined(__clang__) && ((__clang_major__ > 3) || ((__clang_major__ == 3) && (__clang_minor__ >= 7))))
#elif (defined(__clang__) && ((__clang_major__ > 3) || ((__clang_major__ == 3) && (__clang_minor__ >= 7)))) \
|| (defined(__GNUC__) && (__GNUC__ >= 9))
# define RET_NONNULL __attribute__((returns_nonnull))
#else
# define RET_NONNULL
Expand Down
4 changes: 2 additions & 2 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1103,8 +1103,8 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer)

if (mSettings.useSingleJob() || !mSettings.buildDir.empty()) {
// Analyse the tokens..

if (CTU::FileInfo * const fi1 = CTU::getFileInfo(tokenizer)) {
{
CTU::FileInfo * const fi1 = CTU::getFileInfo(tokenizer);
if (!mSettings.buildDir.empty())
mAnalyzerInformation.setFileInfo("ctu", fi1->toString());
if (mSettings.useSingleJob())
Expand Down
4 changes: 2 additions & 2 deletions lib/cppcheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ class CPPCHECKLIB CppCheck : ErrorLogger {
* @brief Returns current version number as a string.
* @return version, e.g. "1.38"
*/
static const char * version();
RET_NONNULL static const char * version();

/**
* @brief Returns extra version info as a string.
* This is for returning extra version info, like Git commit id, build
* time/date etc.
* @return extra version info, e.g. "04d42151" (Git commit id).
*/
static const char * extraVersion();
RET_NONNULL static const char * extraVersion();

/**
* @brief Call all "getErrorMessages" in all registered Check classes.
Expand Down
2 changes: 1 addition & 1 deletion lib/ctu.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ namespace CTU {
CPPCHECKLIB std::string getFunctionId(const Tokenizer &tokenizer, const Function *function);

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

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

Expand Down
4 changes: 2 additions & 2 deletions lib/symboldatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ class CPPCHECKLIB Function {
void isInlineKeyword(bool state) {
setFlag(fIsInlineKeyword, state);
}
const Token *setFlags(const Token *tok1, const Scope *scope);
RET_NONNULL const Token *setFlags(const Token *tok1, const Scope *scope);
};

class CPPCHECKLIB Scope {
Expand Down Expand Up @@ -1440,7 +1440,7 @@ class CPPCHECKLIB SymbolDatabase {
void debugSymbolDatabase() const;

void addClassFunction(Scope *&scope, const Token *&tok, const Token *argStart);
static Function *addGlobalFunctionDecl(Scope*& scope, const Token* tok, const Token *argStart, const Token* funcStart);
RET_NONNULL static Function *addGlobalFunctionDecl(Scope*& scope, const Token* tok, const Token *argStart, const Token* funcStart);
Function *addGlobalFunction(Scope*& scope, const Token*& tok, const Token *argStart, const Token* funcStart);
void addNewFunction(Scope *&scope, const Token *&tok);
bool isFunction(const Token *tok, const Scope* outerScope, const Token *&funcStart, const Token *&argStart, const Token*& declEnd) const;
Expand Down
2 changes: 1 addition & 1 deletion lib/templatesimplifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,7 @@ void TemplateSimplifier::simplifyTemplateAliases()
const Token * const fromStart = args[argnr].first;
const Token * const fromEnd = args[argnr].second->previous();
Token *temp = TokenList::copyTokens(tok1, fromStart, fromEnd, true);
const bool tempOK(temp && temp != tok1->next());
const bool tempOK(temp != tok1->next());
tok1->deleteThis();
if (tempOK)
tok1 = temp; // skip over inserted parameters
Expand Down
3 changes: 0 additions & 3 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2152,9 +2152,6 @@ void Tokenizer::simplifyTypedefCpp()

tok2 = TokenList::copyTokens(tok2, argStart, argEnd);
if (inTemplate) {
if (!tok2)
syntaxError(nullptr);

tok2 = tok2->next();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/tokenize.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class CPPCHECKLIB Tokenizer {
* '; int *p(0);' => '; int *p = 0;'
*/
void simplifyInitVar();
static Token* initVar(Token* tok);
RET_NONNULL static Token* initVar(Token* tok);

/**
* Simplify the location of "static" and "const" qualifiers in
Expand Down
2 changes: 1 addition & 1 deletion lib/tokenlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class CPPCHECKLIB TokenList {
* @param one_line true=>copy all tokens to the same line as dest. false=>copy all tokens to dest while keeping the 'line breaks'
* @return new location of last token copied
*/
static Token *copyTokens(Token *dest, const Token *first, const Token *last, bool one_line = true);
RET_NONNULL static Token *copyTokens(Token *dest, const Token *first, const Token *last, bool one_line = true);

/**
* Create tokens from code.
Expand Down
8 changes: 4 additions & 4 deletions lib/vfvalue.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,10 @@ namespace ValueFlow

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

static const char* toString(MoveKind moveKind);
static const char* toString(LifetimeKind lifetimeKind);
static const char* toString(LifetimeScope lifetimeScope);
static const char* toString(Bound bound);
RET_NONNULL static const char* toString(MoveKind moveKind);
RET_NONNULL static const char* toString(LifetimeKind lifetimeKind);
RET_NONNULL static const char* toString(LifetimeScope lifetimeScope);
RET_NONNULL static const char* toString(Bound bound);

/** How known is this value */
enum class ValueKind : std::uint8_t {
Expand Down
2 changes: 2 additions & 0 deletions lib/xml.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "config.h"
#include "path.h"

SUPPRESS_WARNING_GCC_PUSH("-Wsuggest-attribute=returns_nonnull")
SUPPRESS_WARNING_CLANG_PUSH("-Wzero-as-null-pointer-constant")
SUPPRESS_WARNING_CLANG_PUSH("-Wsuggest-destructor-override")
SUPPRESS_WARNING_CLANG_PUSH("-Winconsistent-missing-destructor-override")
Expand All @@ -33,6 +34,7 @@ SUPPRESS_WARNING_CLANG_POP
SUPPRESS_WARNING_CLANG_POP
SUPPRESS_WARNING_CLANG_POP
SUPPRESS_WARNING_CLANG_POP
SUPPRESS_WARNING_GCC_POP

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

3 comments on commit 164e064

@danmar
Copy link
Owner

@danmar danmar commented on 164e064 Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@firewave this does not work well.

/home/daniel/cppchecksolutions/cppcheck/lib/config.h:186:71: warning: unknown option after ‘#pragma GCC diagnostic’ kind [-Wpragmas]
  186 | #define SUPPRESS_WARNING_PUSH(warning) _Pragma("GCC diagnostic push") _Pragma(STRINGISIZE(GCC diagnostic ignored warning))
      |                                                                       ^~~~~~~
/home/daniel/cppchecksolutions/cppcheck/lib/config.h:188:44: note: in expansion of macro ‘SUPPRESS_WARNING_PUSH’
  188 | #define SUPPRESS_WARNING_GCC_PUSH(warning) SUPPRESS_WARNING_PUSH(warning)
      |                                            ^~~~~~~~~~~~~~~~~~~~~
/home/daniel/cppchecksolutions/cppcheck/lib/xml.h:25:1: note: in expansion of macro ‘SUPPRESS_WARNING_GCC_PUSH’
   25 | SUPPRESS_WARNING_GCC_PUSH("-Wsuggest-attribute=returns_nonnull")
      | ^~~~~~~~~~~~~~~~~~~~~~~~~
/home/daniel/cppchecksolutions/cppcheck/lib/config.h:186:71: note: did you mean ‘-Wsuggest-attribute=noreturn’?
  186 | #define SUPPRESS_WARNING_PUSH(warning) _Pragma("GCC diagnostic push") _Pragma(STRINGISIZE(GCC diagnostic ignored warning))
      |                                                                       ^~~~~~~
/home/daniel/cppchecksolutions/cppcheck/lib/config.h:188:44: note: in expansion of macro ‘SUPPRESS_WARNING_PUSH’
  188 | #define SUPPRESS_WARNING_GCC_PUSH(warning) SUPPRESS_WARNING_PUSH(warning)
      |                                            ^~~~~~~~~~~~~~~~~~~~~
/home/daniel/cppchecksolutions/cppcheck/lib/xml.h:25:1: note: in expansion of macro ‘SUPPRESS_WARNING_GCC_PUSH’
   25 | SUPPRESS_WARNING_GCC_PUSH("-Wsuggest-attribute=returns_nonnull")
      | ^~~~~~~~~~~~~~~~~~~~~~~~~

I get similar warnings in CI also.

@firewave
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danmar Just ignore it for now. I will fix it later today.

I saw this with Cygwin in the CI but not locally. Although it has supported the attribute for quite a while now that specific warning was only added with GCC 14.

@danmar
Copy link
Owner

@danmar danmar commented on 164e064 Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danmar Just ignore it for now. I will fix it later today.

Thanks for fixing it quickly. I will ignore it now then.

Please sign in to comment.