Skip to content

Commit

Permalink
Merge pull request #71 from Matom-ai/DP/format-attribute
Browse files Browse the repository at this point in the history
Add warning attributes to formatting functions
  • Loading branch information
rjonaitis authored Jul 3, 2024
2 parents 6d93bc0 + 87dc1f7 commit 877f391
Show file tree
Hide file tree
Showing 17 changed files with 63 additions and 47 deletions.
2 changes: 1 addition & 1 deletion GUI/chips/LMS7002M/lms7002_mainPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ void lms7002_mainPanel::UpdateVisiblePanel()
currentTab->second->UpdateGUI();
t2 = wxGetUTCTimeMillis();
#ifndef NDEBUG
lime::debug("Visible GUI update time: "s + (t2 - t1).ToString());
lime::debug("Visible GUI update time: %li", (t2 - t1).ToLong());
#endif
}

Expand Down
3 changes: 2 additions & 1 deletion GUI/oglGraph/OpenGLGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,10 @@ void OpenGLGraph::DrawStaticElements()
0,
fheight,
0,
"%s",
settings.titleXaxis.c_str());
m_font->getTextSize(settings.titleYaxis.c_str(), tw, th, fheight);
glRenderText(settings.marginLeft + 1, settings.windowHeight - th - 1, 0, fheight, 0, settings.titleYaxis.c_str());
glRenderText(settings.marginLeft + 1, settings.windowHeight - th - 1, 0, fheight, 0, "%s", settings.titleYaxis.c_str());

double pixelXvalue = 0;
double pixelYvalue = 0;
Expand Down
3 changes: 2 additions & 1 deletion GUI/oglGraph/OpenGLGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,8 @@ class OpenGLGraph : public wxGLCanvas
void switchToWindowView();
void switchToDataView();

GLvoid glRenderText(float posx, float posy, float angle, float scale, unsigned int rgba, const char* fmt, ...);
GLvoid glRenderText
[[gnu::format(printf, 7, 8)]] (float posx, float posy, float angle, float scale, unsigned int rgba, const char* fmt, ...);
int TextWidthInPixels(const char* str);
int NumberWidthInPixels(float num, unsigned int prec = 0);
int LineHeight();
Expand Down
3 changes: 2 additions & 1 deletion embedded/lms7002m/privates.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ void lms7002m_log(lms7002m_context* context, lime_LogLevel level, const char* fo
context->hooks.log(level, buff, context->hooks.log_userData);
}

static void lms7002m_log_va(lms7002m_context* context, lime_LogLevel level, const char* format, va_list args)
static void FORMAT_ATTR(printf, 3, 0)
lms7002m_log_va(lms7002m_context* context, lime_LogLevel level, const char* format, va_list args)
{
if (context->hooks.log == NULL)
return;
Expand Down
11 changes: 9 additions & 2 deletions embedded/lms7002m/privates.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ struct lms7002m_context;
lms7002m_log(context, lime_LogLevel_Debug, format, __VA_ARGS__); \
} while (0)

void lms7002m_log(struct lms7002m_context* context, lime_LogLevel level, const char* format, ...);
lime_Result lms7002m_report_error(struct lms7002m_context* context, lime_Result result, const char* format, ...);
#ifdef __unix__
#define FORMAT_ATTR(type, fmt_str, fmt_param) __attribute__((format(type, fmt_str, fmt_param)))
#else
#define FORMAT_ATTR(type, fmt_str, fmt_param)
#endif

void FORMAT_ATTR(printf, 3, 4) lms7002m_log(struct lms7002m_context* context, lime_LogLevel level, const char* format, ...);
lime_Result FORMAT_ATTR(printf, 3, 4)
lms7002m_report_error(struct lms7002m_context* context, lime_Result result, const char* format, ...);
void lms7002m_sleep(long timeInMicroseconds);

void lms7002m_spi_write(struct lms7002m_context* self, uint16_t address, uint16_t value);
Expand Down
4 changes: 2 additions & 2 deletions plugins/amarisoft-plugin/trx_limesuite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class AmarisoftParamProvider : public LimeSettingsProvider
};

static lime::LogLevel logVerbosity = lime::LogLevel::Debug;
static void Log(LogLevel lvl, const char* format, ...)
static void Log [[gnu::format(printf, 2, 3)]] (LogLevel lvl, const char* format, ...)
{
if (lvl > logVerbosity)
return;
Expand Down Expand Up @@ -235,7 +235,7 @@ static int trx_lms7002m_get_sample_rate(TRXState* s1, TRXFraction* psample_rate,

if (rate < bandwidth)
{
Log(LogLevel::Error, "Port[%i] Manually specified sample rate %i is less than LTE bandwidth %i", p, rate, bandwidth);
Log(LogLevel::Error, "Port[%i] Manually specified sample rate %f is less than LTE bandwidth %i", p, rate, bandwidth);
return -1;
}
Log(LogLevel::Info, "Port[%i] Manually specified sample rate: %f MSps", p, rate / 1e6);
Expand Down
4 changes: 2 additions & 2 deletions src/API/LMS_APIWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ API_EXPORT int CALL_CONV LMS_Calibrate(lms_device_t* device, bool dir_tx, size_t
apiDevice->device->Calibrate(apiDevice->moduleIndex, direction, chan, bw);
} catch (...)
{
lime::error("Failed to calibrate %s channel %i.", ToString(direction).c_str(), chan);
lime::error("Failed to calibrate %s channel %li.", ToString(direction).c_str(), chan);

return -1;
}
Expand Down Expand Up @@ -732,7 +732,7 @@ API_EXPORT int CALL_CONV LMS_SetTestSignal(
apiDevice->device->SetTestSignal(apiDevice->moduleIndex, direction, chan, enumToTestStruct(sig), dc_i, dc_q);
} catch (...)
{
lime::error("Failed to set %s channel %i test signal.", ToString(direction).c_str(), chan);
lime::error("Failed to set %s channel %li test signal.", ToString(direction).c_str(), chan);
}

return 0;
Expand Down
35 changes: 19 additions & 16 deletions src/API/LimePlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ LimePluginContext::LimePluginContext()

enum CalibrateFlag { None = 0, DCIQ = 1, Filter = 2 };

template<class T> static bool GetSetting(LimeSettingsProvider* settings, T* pval, const char* prop_name_format, ...)
template<class T>
static bool GetSetting [[gnu::format(printf, 3, 4)]] (LimeSettingsProvider* settings, T* pval, const char* prop_name_format, ...)
{
char name[256];
va_list args;
Expand All @@ -94,7 +95,8 @@ template<class T> static bool GetSetting(LimeSettingsProvider* settings, T* pval
return true;
}

template<> bool GetSetting(LimeSettingsProvider* settings, std::string* pval, const char* prop_name_format, ...)
template<>
bool GetSetting [[gnu::format(printf, 3, 4)]] (LimeSettingsProvider* settings, std::string* pval, const char* prop_name_format, ...)
{
char name[256];
va_list args;
Expand All @@ -105,7 +107,8 @@ template<> bool GetSetting(LimeSettingsProvider* settings, std::string* pval, co
return settings->GetString(*pval, name);
}

template<class T> static bool GetParam(LimePluginContext* context, T& pval, const char* prop_name_format, ...)
template<class T>
static bool GetParam [[gnu::format(printf, 3, 4)]] (LimePluginContext* context, T& pval, const char* prop_name_format, ...)
{
char name[256];
va_list args;
Expand All @@ -122,7 +125,7 @@ template<class T> static bool GetParam(LimePluginContext* context, T& pval, cons

static lime::LogLevel logVerbosity = lime::LogLevel::Debug;

static void Log(LogLevel lvl, const char* format, ...)
static void Log [[gnu::format(printf, 2, 3)]] (LogLevel lvl, const char* format, ...)
{
if (lvl > logVerbosity)
return;
Expand Down Expand Up @@ -167,7 +170,7 @@ bool OnStreamStatusChange(bool isTx, const StreamStats* s, void* userData)
<< " MB/s"sv
<< "\nTx| Late: "sv << status.tx.loss << " underrun: "sv << status.tx.underrun << " rate: "sv
<< status.tx.dataRate_Bps / 1e6 << " MB/s"sv;
Log(LogLevel::Warning, ss.str().c_str());
Log(LogLevel::Warning, "%s", ss.str().c_str());
lastStreamUpdate = now;
}
return false;
Expand Down Expand Up @@ -413,7 +416,7 @@ static OpStatus LoadDevicesConfigurationFile(LimePluginContext* context)
const auto& desc = node.device->GetDescriptor();
if (node.chipIndex >= desc.rfSOC.size())
{
Log(LogLevel::Error, "Invalid chipIndex (%i). dev%i has only %i chips.", node.chipIndex, i, desc.rfSOC.size());
Log(LogLevel::Error, "Invalid chipIndex (%i). dev%li has only %li chips.", node.chipIndex, i, desc.rfSOC.size());
return OpStatus::OutOfRange;
}

Expand All @@ -430,12 +433,12 @@ static OpStatus LoadDevicesConfigurationFile(LimePluginContext* context)

if (chip->LoadConfig(configFilepath, false) != OpStatus::Success)
{
Log(LogLevel::Error, "dev%s chip%i Error loading file: %s", i, node.chipIndex, configFilepath.c_str());
Log(LogLevel::Error, "dev%li chip%i Error loading file: %s", i, node.chipIndex, configFilepath.c_str());
return OpStatus::Error;
}

node.config.skipDefaults = true;
Log(LogLevel::Info, "dev%i chip%i loaded with: %s", i, node.chipIndex, configFilepath.c_str());
Log(LogLevel::Info, "dev%li chip%i loaded with: %s", i, node.chipIndex, configFilepath.c_str());
}
return OpStatus::Success;
}
Expand Down Expand Up @@ -527,7 +530,7 @@ static void GatherDeviceTreeNodeSettings(LimePluginContext* context, LimeSetting
DevNode& dev = context->rfdev.at(i);
char devPrefix[16];
std::snprintf(devPrefix, sizeof(devPrefix), "dev%i", i);
GetSetting(settings, &dev.handleString, devPrefix);
GetSetting(settings, &dev.handleString, "%s", devPrefix);
GetSetting(settings, &dev.chipIndex, "%s_chip_index", devPrefix);

GatherConfigSettings(&dev.configInputs, settings, devPrefix);
Expand All @@ -544,7 +547,7 @@ static OpStatus GatherPortSettings(LimePluginContext* context, LimeSettingsProvi
char portPrefix[16];
std::snprintf(portPrefix, sizeof(portPrefix), "port%i", i);
GatherConfigSettings(&port.configInputs, settings, portPrefix);
if (GetSetting(settings, &port.deviceNames, portPrefix))
if (GetSetting(settings, &port.deviceNames, "%s", portPrefix))
{
++specifiedPortsCount;
OpStatus status = AssignDevicesToPorts(context);
Expand Down Expand Up @@ -789,7 +792,7 @@ static void TransferRuntimeParametersToConfig(
params.freq[i] / 1.0e6,
trxConfig.centerFrequency / 1.0e6,
(trxConfig.centerFrequency - params.freq[i]) / 1.0e6);
Log(LogLevel::Info, loFreqStr);
Log(LogLevel::Info, "%s", loFreqStr);
}

if (trxConfig.gfir.bandwidth == 0) // update only if not set by settings file
Expand All @@ -802,7 +805,7 @@ static void TransferRuntimeParametersToConfig(
const auto& paths = desc.pathNames.at(isTx ? TRXDir::Tx : TRXDir::Rx);

Log(LogLevel::Verbose,
"%s channel%i: dev%i chip%i ch%i , LO: %.3f MHz SR: %.3f MHz BW: %.3f MHz | path: %i('%s')",
"%s channel%li: dev%i chip%i ch%i , LO: %.3f MHz SR: %.3f MHz BW: %.3f MHz | path: %i('%s')",
isTx ? "Tx" : "Rx",
i,
channelMap[i].parent->devIndex,
Expand Down Expand Up @@ -860,7 +863,7 @@ OpStatus ConfigureStreaming(LimePluginContext* context, const LimeRuntimeParamet
continue;

Log(LogLevel::Debug,
"Port[%i] Stream samples format: %s , link: %s %s",
"Port[%li] Stream samples format: %s , link: %s %s",
p,
stream.format == DataFormat::F32 ? "F32" : "I16",
stream.linkFormat == DataFormat::I12 ? "I12" : "I16",
Expand All @@ -869,7 +872,7 @@ OpStatus ConfigureStreaming(LimePluginContext* context, const LimeRuntimeParamet
port.composite = new StreamComposite(aggregates);
if (port.composite->StreamSetup(stream) != OpStatus::Success)
{
Log(LogLevel::Error, "Port%i stream setup failed.", p);
Log(LogLevel::Error, "Port%li stream setup failed.", p);
return OpStatus::Error;
}
}
Expand Down Expand Up @@ -898,12 +901,12 @@ int LimePlugin_Setup(LimePluginContext* context, const LimeRuntimeParameters* pa
continue;
else if (node.device != nullptr && !node.assignedToPort)
{
Log(LogLevel::Warning, "dev%i is not assigned to any port.", i);
Log(LogLevel::Warning, "dev%li is not assigned to any port.", i);
continue;
}
try
{
Log(LogLevel::Debug, "dev%i configure.", i);
Log(LogLevel::Debug, "dev%li configure.", i);
OpStatus status = node.device->Configure(node.config, node.chipIndex);
if (status != OpStatus::Success)
return -1;
Expand Down
2 changes: 1 addition & 1 deletion src/CommonFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace lime {

const std::string strFormat(const char* format, ...);
const std::string strFormat [[gnu::format(printf, 1, 2)]] (const char* format, ...);

template<typename T> std::string intToHex(T i)
{
Expand Down
2 changes: 1 addition & 1 deletion src/comms/PCIe/linux-kernel-module/litepcie.c
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,7 @@ static void litepcie_free_chdev(struct litepcie_device *s)
}

/* from stackoverflow */
void sfind(char *string, char *format, ...)
void __attribute__((format(scanf, 2, 3))) sfind(char *string, char *format, ...)
{
va_list arglist;

Expand Down
2 changes: 1 addition & 1 deletion src/comms/USB/USBGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ int32_t USBGeneric::BulkTransfer(uint8_t endPointAddr, uint8_t* data, size_t len
int status = libusb_bulk_transfer(dev_handle, endPointAddr, data, length, &actualTransferred, timeout_ms);
if (status != 0)
{
lime::error("USBGeneric::BulkTransfer(0x%02X) : %s, transferred: %i, expected: %i",
lime::error("USBGeneric::BulkTransfer(0x%02X) : %s, transferred: %i, expected: %lu",
endPointAddr,
libusb_error_name(status),
actualTransferred,
Expand Down
2 changes: 1 addition & 1 deletion src/examples/dualRXTX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ int main(int argc, char** argv)
#ifdef USE_GNU_PLOT
gp.write("plot '-' with points title 'ch 0'");
for (std::size_t c = 1; c < stream.channels.at(TRXDir::Rx).size(); ++c)
gp.writef(", '-' with points title 'ch %i'\n", c);
gp.writef(", '-' with points title 'ch %lu'\n", c);
for (std::size_t c = 0; c < stream.channels.at(TRXDir::Rx).size(); ++c)
{
for (uint32_t n = 0; n < samplesInBuffer; ++n)
Expand Down
7 changes: 5 additions & 2 deletions src/gnuPlotPipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class GNUPlotPipe
/// @brief Writes formatted data into the pipe.
/// @param format The format string.
/// @param ... The format arguments parameters.
void writef(const char* format, ...)
void writef [[gnu::format(printf, 2, 3)]] (const char* format, ...)
{
static char temp[512];
va_list argList;
Expand All @@ -58,7 +58,10 @@ class GNUPlotPipe
}

/// @brief Flushes the pipe.
void flush() { fflush(pipeHandle); }
void flush()
{
fflush(pipeHandle);
}

protected:
FILE* pipeHandle;
Expand Down
16 changes: 8 additions & 8 deletions src/include/limesuiteng/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,23 @@ LIME_API const char* GetLastErrorMessageCString(void);
LIME_API const std::string& GetLastErrorMessage(void);

// C-string versions
LIME_API void critical(const char* format, ...); //!< Log a critical error message with formatting
LIME_API void critical [[gnu::format(printf, 1, 2)]] (const char* format, ...); //!< Log a critical error message with formatting
LIME_API void critical(const std::string& text); //!< Log a critical error message

LIME_API int error(const char* format, ...); //!< Log an error message with formatting
LIME_API int error [[gnu::format(printf, 1, 2)]] (const char* format, ...); //!< Log an error message with formatting
LIME_API int error(const std::string& text); //!< Log an error message

LIME_API void warning(const char* format, ...); //!< Log a warning message with formatting
LIME_API void warning [[gnu::format(printf, 1, 2)]] (const char* format, ...); //!< Log a warning message with formatting
LIME_API void warning(const std::string& text); //!< Log a warning message

LIME_API void info(const char* format, ...); //!< Log an information message with formatting
LIME_API void info [[gnu::format(printf, 1, 2)]] (const char* format, ...); //!< Log an information message with formatting
LIME_API void info(const std::string& text); //!< Log an information message

LIME_API void debug(const char* format, ...); //!< Log a debug message with formatting
LIME_API void debug [[gnu::format(printf, 1, 2)]] (const char* format, ...); //!< Log a debug message with formatting
LIME_API void debug(const std::string& text); //!< Log a debug message

//! Log a message with formatting and specified logging level
LIME_API void log(const LogLevel level, const char* format, ...);
LIME_API void log [[gnu::format(printf, 2, 3)]] (const LogLevel level, const char* format, ...);
//! Log a message with specified logging level
LIME_API void log(const LogLevel level, const std::string& text);

Expand All @@ -77,10 +77,10 @@ LIME_API OpStatus ReportError(const OpStatus errnum);
* \param format a format string followed by args
* \return passthrough errnum
*/
LIME_API OpStatus ReportError(const OpStatus errnum, const char* format, ...);
LIME_API OpStatus ReportError [[gnu::format(printf, 2, 3)]] (const OpStatus errnum, const char* format, ...);
LIME_API OpStatus ReportError(const OpStatus errnum, const std::string& text);

LIME_API int ReportError(const int errnum, const char* format, ...);
LIME_API int ReportError [[gnu::format(printf, 2, 3)]] (const int errnum, const char* format, ...);
LIME_API int ReportError(const int errnum, const std::string& text);

} // namespace lime
Expand Down
8 changes: 4 additions & 4 deletions src/logger/LoggerCString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const char* GetLastErrorMessageCString(void)
return Logger::_reportedErrorMessage.c_str();
}

void log(const LogLevel level, const char* format, va_list argList)
void log [[gnu::format(printf, 2, 0)]] (const LogLevel level, const char* format, va_list argList)
{
char buff[4096];
int ret = std::vsnprintf(buff, sizeof(buff), format, argList);
Expand Down Expand Up @@ -87,7 +87,7 @@ void log(const LogLevel level, const char* format, ...)

static constexpr std::size_t MAX_MSG_LEN = 1024;
thread_local char _reportedErrorMessageCStringBuffer[MAX_MSG_LEN];
template<class T> inline T ReportErrorTemplate(const T errnum, const char* format, va_list argList)
template<class T> inline T ReportErrorTemplate [[gnu::format(printf, 2, 0)]] (const T errnum, const char* format, va_list argList)
{
Logger::_reportedErrorCode = static_cast<int>(errnum);

Expand All @@ -101,12 +101,12 @@ template<class T> inline T ReportErrorTemplate(const T errnum, const char* forma
return errnum;
}

int ReportError(const int errnum, const char* format, va_list argList)
int ReportError [[gnu::format(printf, 2, 0)]] (const int errnum, const char* format, va_list argList)
{
return ReportErrorTemplate(errnum, format, argList);
}

OpStatus ReportError(const OpStatus errnum, const char* format, va_list argList)
OpStatus ReportError [[gnu::format(printf, 2, 0)]] (const OpStatus errnum, const char* format, va_list argList)
{
return ReportErrorTemplate(errnum, format, argList);
}
Expand Down
2 changes: 1 addition & 1 deletion src/protocols/TRXLooper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ void TRXLooper::ReceivePacketsLoop()
mRx.memPool->Allocate(outputPktSize), samplesInPkt * mRxArgs.packetsToBatch, outputSampleSize);
if (outputPkt == nullptr)
{
lime::warning("Rx%i: packets fifo full.");
lime::warning("Rx%i: packets fifo full.", chipId);
continue;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/threadHelper/threadHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ int lime::SetOSThreadPriority(ThreadPriority priority, ThreadPolicy policy, std:
if (int ret = pthread_setschedparam(thread->native_handle(), sched_policy, &sch))
{
lime::debug("SetOSThreadPriority: Failed to set priority(%d), sched_prio(%d), policy(%d), ret(%d)",
priority,
static_cast<int>(priority),
sched_policy,
ret,
sch.sched_priority);
Expand Down Expand Up @@ -99,7 +99,7 @@ int lime::SetOSCurrentThreadPriority(ThreadPriority priority, ThreadPolicy polic
if (int ret = pthread_setschedparam(pthread_self(), sched_policy, &sch) != 0)
{
lime::debug("SetOSCurrentThreadPriority: Failed to set priority(%d), sched_prio(%d), policy(%d), ret(%d)",
priority,
static_cast<int>(priority),
sched_policy,
ret,
sch.sched_priority);
Expand Down

0 comments on commit 877f391

Please sign in to comment.