GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — handling of defined() conditions nested in function like macros · Issue #75 · cppcheck-opensource/simplecpp

handling of defined() conditions nested in function like macros #75

New issue
Copy link
New issue
Copy link
Open
Open
handling of defined() conditions nested in function like macros#75
Copy link
Labels
bug

Description

@illu89
illu89
opened on Jun 1, 2017
Issue body actions

defined() conditions in function like macros are not handled correctly.

minimal example, which expects __FOO__ to be defined or not.

#include <stdio.h>
#define macro(var) (defined(__ ## var ## __))

int main(void) {
  #if macro(FOO)
    printf("__FOO__ defined\r\n");
  #else
    printf("__FOO__ not defined\r\n");
  #endif
  return 0;
}

preprocessing with gcc gives:

# gcc  -D__FOO__ -E test.c
[...]
int main (void)  {
    printf("__FOO__ defined\r\n");
    return 0;
}

preprocessing with simplecpp gives

# simplecpp -D__FOO__ -E test.c
int main (  void  )  {
    printf( "__FOO__ not defined\r\n" ) ;
    return 0 ;
}

The real world code i encountered the problem with is in parts.h of atmel software framework. They expect the user to define the chip, software is being compiled for, as __chipname__ and derive family, series and product line from this.

short excerpt to clarify:

#define SAM_PART_IS_DEFINED(part) (defined(__ ## part ## __))
 [...]

/* SAM4S series */
#define SAM4S16 ( \
        SAM_PART_IS_DEFINED(SAM4S16B) || \
        SAM_PART_IS_DEFINED(SAM4S16C) \
        )
[...]

/* SAM4S Family */
#define SAM4S (SAM4S2 || SAM4S4 || SAM4S8 || SAM4S16 || SAM4SA16 || SAM4SD16 || SAM4SD32)
[...]

/* SAM product line */
#define SAM (SAM3S || SAM3U || SAM3N || SAM3XA || SAM4S || SAM4L || SAM4E || \
        SAM0 || SAM4N || SAM4C || SAM4CM || SAM4CP || SAMG)

So if __SAM4S16C__ was defined by -D__SAM4S16C__ the macros SAM4S16, SAM4S and SAM are expected to be set to 1

Activity

  1. danmar
    added
    bug
    on Jul 2, 2020
  2. firewave commented on Jul 3, 2025

    @firewave
    firewave
    on Jul 3, 2025
    Collaborator
    More actions

    It now reports:

    a.cpp:5: syntax error: failed to evaluate #if condition, undefined function-like macro invocation: defined( ... )
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.