Skip to content

Compilation fails due to function reordering #500

Open
@Floessie

Description

@Floessie

Describe the problem

In order to make it easier for beginners to get started with writing Arduino sketches, and for the convenience of all users, Arduino CLI automatically generates and adds prototypes for functions defined in a .ino file of a sketch.

If the user has already manually added function prototypes to the sketch, generation is skipped for those functions.

🐛 When the following sketch code is compiled, Arduino CLI generates prototypes even though they are already present and injects them at inappropriate locations:

namespace
{

    struct State {
    };

    struct Configuration {
        enum class AutoMode : uint8_t {
            INDEPENDENT,
            LINKED
        };
    };

    struct StatsMinMax {
    };

    struct StatsDurations {
    };

    enum class Command : uint8_t {
        GET_STATE,
        GET_CONFIG,
        GET_STATS_MIN_MAX,
        GET_STATS_DURATIONS
    };

    void dump(const StatsMinMax& stats_min_max, const StatsDurations& stats_durations)
    {
    }

    template<typename T>
    Command getCommand(const T& type);

    template<>
    Command getCommand(const State& type)
    {
        return Command::GET_STATE;
    }

    template<>
    Command getCommand(const Configuration& type)
    {
        return Command::GET_CONFIG;
    }

    template<>
    Command getCommand(const StatsMinMax& type)
    {
        return Command::GET_STATS_MIN_MAX;
    }

    template<>
    Command getCommand(const StatsDurations& type)
    {
        return Command::GET_STATS_DURATIONS;
    }

}

void setup()
{
}

void loop()
{
}

The output is:

sketch_nov27a:33:49: error: 'getCommand' is not a template function
 
                                                 ^
sketch_nov27a:39:57: error: 'getCommand' is not a template function
 
                                                         ^
sketch_nov27a:45:55: error: 'getCommand' is not a template function
 
                                                       ^
sketch_nov27a:51:58: error: 'getCommand' is not a template function
 
                                                          ^
exit status 1
'getCommand' is not a template function

Seems like the template function prototype isn't picked up. Removing dump() makes it compile again as does removing the enum class inside Configuration. A real workaround is to define the prototype like so:

template<typename T>
Command getCommand(const T& type)
{
}

To reproduce

Compile the sketch I provided above for any board.

Expected behavior

Function prototypes are not generated when they are already present in the sketch code.

Arduino CLI version

Original report

Arduino IDE 1.8.10

Last verified with

20c9dd4

Additional context

The code compiles successfully when using Arduino IDE 1.8.5.


As template function prototype declaration is normally (speaking for C++) sufficient it would be nice if the Arduino toolchain could cope with it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions