Skip to content

Commit cc94a04

Browse files
committed
Function attributes for pure and const
1 parent 655ac16 commit cc94a04

File tree

5 files changed

+34
-18
lines changed

5 files changed

+34
-18
lines changed

src/c-lib/ai.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ static bool populateAIlengthByPrefix(gs1_encoder* const ctx) {
102102
return true;
103103
}
104104

105-
static inline uint8_t aiLengthByPrefix(const gs1_encoder* const ctx, const char *ai) {
105+
static inline __ATTR_PURE uint8_t aiLengthByPrefix(const gs1_encoder* const ctx, const char *ai) {
106106
assert(ai[0] >= '0' && ai[0] <= '9' && ai[1] >= '0' && ai[1] <= '9');
107107
return ctx->aiLengthByPrefix[(ai[0] - '0') * 10 + (ai[1] - '0')];
108108
}
@@ -200,7 +200,7 @@ static const uint8_t fixedAIprefixLengths[100] = {
200200
VL, VL, VL, VL, VL, VL, VL, VL, VL, VL,
201201
};
202202

203-
static inline uint8_t valLengthByPrefix(const char* const ai) {
203+
static inline __ATTR_PURE uint8_t valLengthByPrefix(const char* const ai) {
204204
assert(ai[0] >= '0' && ai[0] <= '9' && ai[1] >= '0' && ai[1] <= '9');
205205
return fixedAIprefixLengths[(ai[0] - '0') * 10 + (ai[1] - '0')];
206206
}
@@ -411,13 +411,13 @@ static size_t validate_ai_val(gs1_encoder* const ctx, const char* const ai, cons
411411
* Return the overall minimum and maximum lengths for an AI, by summing the components.
412412
*
413413
*/
414-
static inline size_t aiEntryMinLength(const struct aiEntry* const entry) {
414+
static inline __ATTR_PURE size_t aiEntryMinLength(const struct aiEntry* const entry) {
415415
const struct aiComponent *part;
416416
size_t l;
417417
for (part = entry->parts, l = 0; part->cset; l+= (part->opt == MAN ? part->min : 0), part++);
418418
return l;
419419
}
420-
static inline size_t aiEntryMaxLength(const struct aiEntry* const entry) {
420+
static inline __ATTR_PURE size_t aiEntryMaxLength(const struct aiEntry* const entry) {
421421
const struct aiComponent *part;
422422
size_t l;
423423
for (part = entry->parts, l = 0; part->cset; l+= part->max, part++);

src/c-lib/enc-private.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,16 @@
3838
#define strdup _strdup
3939
#endif
4040

41-
#define SIZEOF_ARRAY(x) (sizeof(x) / sizeof(x[0]))
41+
#if defined(__GNUC__) || defined(__clang__)
42+
#define __ATTR_CONST __attribute__ ((__const__))
43+
#define __ATTR_PURE __attribute__ ((__pure__))
44+
#elif _MSC_VER
45+
#define __ATTR_CONST __declspec(noalias)
46+
#define __ATTR_PURE
47+
#else
48+
#define __ATTR_CONST
49+
#define __ATTR_PURE
50+
#endif
4251

4352
#if defined(__clang__)
4453
# define DIAG_PUSH _Pragma("clang diagnostic push")
@@ -54,6 +63,7 @@
5463
# define DIAG_DISABLE_DEPRECATED_DECLARATIONS __pragma(warning(disable: 4996))
5564
#endif
5665

66+
#define SIZEOF_ARRAY(x) (sizeof(x) / sizeof(x[0]))
5767

5868

5969
#include "ai.h"

src/c-lib/gs1encoders-test.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,20 @@
2424
* Don't perform code analysis of third-party code that is only used for testing.
2525
*
2626
*/
27-
#if defined(_MSC_VER)
28-
#include <CodeAnalysis/warnings.h>
29-
#pragma warning(push)
30-
#pragma warning(disable: ALL_CODE_ANALYSIS_WARNINGS)
27+
#if defined(__clang__)
28+
#elif defined(__GNUC__)
29+
# pragma GCC diagnostic ignored "-Wsuggest-attribute=pure"
30+
#elif defined(_MSC_VER)
31+
# include <CodeAnalysis/warnings.h>
32+
# pragma warning(push)
33+
# pragma warning(disable: ALL_CODE_ANALYSIS_WARNINGS)
3134
#endif
3235
#include "acutest.h"
33-
#if defined(_MSC_VER)
34-
#pragma warning(pop)
36+
#if defined(__clang__)
37+
#elif defined(__GNUC__)
38+
# pragma GCC diagnostic pop
39+
#elif defined(_MSC_VER)
40+
# pragma warning(pop)
3541
#endif
3642

3743
#include <stddef.h>

src/c-lib/gs1encoders.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ static inline void reset_error(gs1_encoder* const ctx) {
4242
}
4343

4444

45-
size_t gs1_encoder_instanceSize(void) {
45+
__ATTR_CONST size_t gs1_encoder_instanceSize(void) {
4646
return sizeof(struct gs1_encoder);
4747
}
4848

4949

50-
int gs1_encoder_getMaxDataStrLength(void) {
50+
__ATTR_CONST int gs1_encoder_getMaxDataStrLength(void) {
5151
return MAX_DATA;
5252
}
5353

@@ -109,7 +109,7 @@ void gs1_encoder_free(gs1_encoder* const ctx) {
109109
}
110110

111111

112-
char* gs1_encoder_getVersion(void) {
112+
__ATTR_CONST char* gs1_encoder_getVersion(void) {
113113
return __DATE__;
114114
}
115115

@@ -582,14 +582,14 @@ void gs1_encoder_copyDLignoredQueryParams(gs1_encoder* const ctx, void* const bu
582582
}
583583

584584

585-
char* gs1_encoder_getErrMsg(gs1_encoder* const ctx) {
585+
__ATTR_PURE char* gs1_encoder_getErrMsg(gs1_encoder* const ctx) {
586586
assert(ctx);
587587
assert((!ctx->errFlag) ^ *ctx->errMsg);
588588
return ctx->errMsg;
589589
}
590590

591591

592-
char* gs1_encoder_getErrMarkup(gs1_encoder* const ctx) {
592+
__ATTR_PURE char* gs1_encoder_getErrMarkup(gs1_encoder* const ctx) {
593593
assert(ctx);
594594
return ctx->linterErrMarkup;
595595
}
@@ -600,7 +600,7 @@ char* gs1_encoder_getErrMarkup(gs1_encoder* const ctx) {
600600
*
601601
*/
602602

603-
bool gs1_allDigits(const uint8_t* const str, size_t len) {
603+
__ATTR_PURE bool gs1_allDigits(const uint8_t* const str, size_t len) {
604604

605605
size_t i;
606606

src/c-lib/scandata.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ static const struct symIdEntry symIdTable[] = {
8888
#define CC_SYM_ID "]e0"
8989

9090

91-
static const char* lookupSymId(const gs1_encoder* const ctx) {
91+
static const __ATTR_PURE char* lookupSymId(const gs1_encoder* const ctx) {
9292

9393
size_t i;
9494
const char *symId = NULL;

0 commit comments

Comments
 (0)