arduino / arduino-preprocessor

Parses an Arduino Sketch and converts it into valid C++ source code
49 stars 13 forks source link

Forward declaration not generated when function has default parameter values #12

Open Pwuts opened 4 years ago

Pwuts commented 4 years ago

It goes for very simple functions; the following function is not included in the forward declarations:

void test(int t = 1)
{
    Serial.print(t);
}

... while the function below is included in the forward declarations:

void test(int t)
{
    Serial.print(t);
}

Tested in the Arduino IDE, with PlatformIO, and with the standalone arduino-preprocessor.exe which I downloaded from the releases page.

As a result of this bug, I get errors at compile time, like this:

Metertrekker2MQTT.ino:223:60: error: 'mqtt_publish' was not declared in this scope

Debug info First part of the preprocessor's output:

Variable CRC declared at 13:1 (range 13:1 to 13:7)
Variable d_mqtt_host declared at 4:1 (range 4:1 to 4:28)
Variable d_mqtt_port declared at 5:1 (range 5:1 to 5:28)
Variable d_mqtt_topic_root declared at 6:1 (range 6:1 to 6:33)
Variable d_notify_topic declared at 7:1 (range 7:1 to 7:30)
Variable d_client_id declared at 8:1 (range 8:1 to 8:28)
Variable d_influx_topic declared at 14:3 (range 14:3 to 14:32)
Variable d_influx_electricity_measurement declared at 15:3 (range 15:3 to 15:50)
Variable d_influx_gas_measurement declared at 16:3 (range 16:3 to 16:42)
Variable d_interval declared at 24:1 (range 24:1 to 24:33)
Variable d_timeout declared at 25:1 (range 25:1 to 25:32)
Variable metricDefs declared at 62:1 (range 62:1 to 109:1)
Variable espClient declared at 17:1 (range 17:1 to 17:12)
Variable mqtt_client declared at 18:1 (range 18:1 to 18:14)
Variable mqtt_host declared at 20:1 (range 20:1 to 20:8)
Variable mqtt_port declared at 21:1 (range 21:1 to 21:5)
Variable mqtt_topic_root declared at 22:1 (range 22:1 to 22:8)
Variable mqtt_notify_topic declared at 23:1 (range 23:1 to 23:8)
Variable influx_topic declared at 26:5 (range 26:5 to 26:12)
Variable P1 declared at 29:1 (range 29:1 to 29:16)
Function set_RTS declared at 51:1 (range 51:1 to 55:1)
Variable lastTelegram declared at 63:1 (range 63:1 to 63:6)
Variable interval declared at 64:1 (range 64:1 to 64:14)
Variable timeout declared at 65:1 (range 65:1 to 65:14)
Function setup declared at 67:1 (range 67:1 to 88:1)
Variable bufferIn declared at 91:1 (range 91:1 to 91:18)
Variable readLength declared at 92:1 (range 92:1 to 92:8)
Variable receivedCRC declared at 93:1 (range 93:1 to 93:19)
Function loop declared at 95:1 (range 95:1 to 143:1)
Function requestTelegram declared at 146:1 (range 146:1 to 161:1)
Function timeoutHandler declared at 164:1 (range 164:1 to 168:1)
Function verifyTelegram declared at 171:1 (range 171:1 to 180:1)
Function parseTelegram declared at 183:1 (range 183:1 to 375:1)
Function getMetricDef declared at 378:1 (range 378:1 to 387:1)
Function mqtt_publish declared at 390:1 (range 390:1 to 393:1)
Function appendInfluxValue declared at 397:1 (range 397:1 to 401:1)
Function setup_wifi declared at 407:1 (range 407:1 to 445:1)
Function setup_ota declared at 448:1 (range 448:1 to 453:1)
Function connect_mqtt declared at 455:1 (range 455:1 to 478:1)

The resulting forward declarations:

void set_RTS(bool s);
void setup();
void loop();
void requestTelegram();
void timeoutHandler();
bool verifyTelegram(const byte* telegram, const char* checkCRC);
void parseTelegram(char* telegram);
metricDef* getMetricDef(const char* ident);
void appendInfluxValue(String* influxString, char* column_name, String value, bool valueIsString);
void setup_wifi();
void setup_ota();
void connect_mqtt();

As you can see, mqtt_publish is missing in the forward declarations.

bool mqtt_publish(const String topic_path, const String message, bool retain = false)
{
    return mqtt_client.publish((mqtt_topic_root + topic_path).c_str(), message.c_str(), retain);
}
Pwuts commented 4 years ago

When fixing this issue, the function header must not only be added as a forward declaration, but the default arguments have to be removed from the function definition, otherwise the compiler will throw an error like error: default argument given for parameter 1 of 'void test(int t)'.

So this

/* [other code] */
void test(int t = 1)
{
    Serial.print(t);
}

becomes

void test(int t = 1);
/* [other code] */
void test(int t)
{
    Serial.print(t);
}