Skip to content

Commit 3aefaab

Browse files
authored
check C++ code with -Wmissing-declarations (#3184)
1 parent 69eb67e commit 3aefaab

File tree

22 files changed

+247
-243
lines changed

22 files changed

+247
-243
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ if (LLAMA_ALL_WARNINGS)
427427
-Wextra
428428
-Wpedantic
429429
-Wcast-qual
430+
-Wmissing-declarations
430431
-Wno-unused-function
431432
-Wno-multichar
432433
)

Makefile

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,16 @@ endif # LLAMA_DISABLE_LOGS
172172
# warnings
173173
MK_CFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wdouble-promotion -Wshadow -Wstrict-prototypes -Wpointer-arith \
174174
-Wmissing-prototypes -Werror=implicit-int -Wno-unused-function
175-
MK_CXXFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function -Wno-multichar
175+
MK_CXXFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wmissing-declarations -Wno-unused-function -Wno-multichar
176176

177-
ifeq '' '$(findstring clang,$(shell $(CXX) --version))'
177+
# TODO(cebtenzzre): remove this once PR #2632 gets merged
178+
TTFS_CXXFLAGS = $(CXXFLAGS) -Wno-missing-declarations
179+
180+
ifneq '' '$(findstring clang,$(shell $(CXX) --version))'
181+
# clang++ only
182+
MK_CXXFLAGS += -Wmissing-prototypes
183+
TTFS_CXXFLAGS += -Wno-missing-prototypes
184+
else
178185
# g++ only
179186
MK_CXXFLAGS += -Wno-format-truncation -Wno-array-bounds
180187
endif
@@ -524,7 +531,7 @@ gguf: examples/gguf/gguf.cpp ggml.o llama.o $(OBJS)
524531
$(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
525532

526533
train-text-from-scratch: examples/train-text-from-scratch/train-text-from-scratch.cpp ggml.o llama.o common.o $(OBJS)
527-
$(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
534+
$(CXX) $(TTFS_CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
528535

529536
convert-llama2c-to-ggml: examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp ggml.o llama.o $(OBJS)
530537
$(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)

common/common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ int32_t get_num_physical_cores() {
7878
return n_threads > 0 ? (n_threads <= 4 ? n_threads : n_threads / 2) : 4;
7979
}
8080

81-
void process_escapes(std::string& input) {
81+
static void process_escapes(std::string& input) {
8282
std::size_t input_len = input.length();
8383
std::size_t output_idx = 0;
8484

common/console.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ namespace console {
158158
}
159159
}
160160

161-
char32_t getchar32() {
161+
static char32_t getchar32() {
162162
#if defined(_WIN32)
163163
HANDLE hConsole = GetStdHandle(STD_INPUT_HANDLE);
164164
wchar_t high_surrogate = 0;
@@ -212,7 +212,7 @@ namespace console {
212212
#endif
213213
}
214214

215-
void pop_cursor() {
215+
static void pop_cursor() {
216216
#if defined(_WIN32)
217217
if (hConsole != NULL) {
218218
CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
@@ -233,7 +233,7 @@ namespace console {
233233
putc('\b', out);
234234
}
235235

236-
int estimateWidth(char32_t codepoint) {
236+
static int estimateWidth(char32_t codepoint) {
237237
#if defined(_WIN32)
238238
(void)codepoint;
239239
return 1;
@@ -242,7 +242,7 @@ namespace console {
242242
#endif
243243
}
244244

245-
int put_codepoint(const char* utf8_codepoint, size_t length, int expectedWidth) {
245+
static int put_codepoint(const char* utf8_codepoint, size_t length, int expectedWidth) {
246246
#if defined(_WIN32)
247247
CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
248248
if (!GetConsoleScreenBufferInfo(hConsole, &bufferInfo)) {
@@ -303,7 +303,7 @@ namespace console {
303303
#endif
304304
}
305305

306-
void replace_last(char ch) {
306+
static void replace_last(char ch) {
307307
#if defined(_WIN32)
308308
pop_cursor();
309309
put_codepoint(&ch, 1, 1);
@@ -312,7 +312,7 @@ namespace console {
312312
#endif
313313
}
314314

315-
void append_utf8(char32_t ch, std::string & out) {
315+
static void append_utf8(char32_t ch, std::string & out) {
316316
if (ch <= 0x7F) {
317317
out.push_back(static_cast<unsigned char>(ch));
318318
} else if (ch <= 0x7FF) {
@@ -333,7 +333,7 @@ namespace console {
333333
}
334334

335335
// Helper function to remove the last UTF-8 character from a string
336-
void pop_back_utf8_char(std::string & line) {
336+
static void pop_back_utf8_char(std::string & line) {
337337
if (line.empty()) {
338338
return;
339339
}
@@ -349,7 +349,7 @@ namespace console {
349349
line.erase(pos);
350350
}
351351

352-
bool readline_advanced(std::string & line, bool multiline_input) {
352+
static bool readline_advanced(std::string & line, bool multiline_input) {
353353
if (out != stdout) {
354354
fflush(stdout);
355355
}
@@ -452,7 +452,7 @@ namespace console {
452452
return has_more;
453453
}
454454

455-
bool readline_simple(std::string & line, bool multiline_input) {
455+
static bool readline_simple(std::string & line, bool multiline_input) {
456456
#if defined(_WIN32)
457457
std::wstring wline;
458458
if (!std::getline(std::wcin, wline)) {

common/grammar-parser.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace grammar_parser {
1010
// NOTE: assumes valid utf8 (but checks for overrun)
1111
// copied from llama.cpp
12-
std::pair<uint32_t, const char *> decode_utf8(const char * src) {
12+
static std::pair<uint32_t, const char *> decode_utf8(const char * src) {
1313
static const int lookup[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 4 };
1414
uint8_t first_byte = static_cast<uint8_t>(*src);
1515
uint8_t highbits = first_byte >> 4;
@@ -24,19 +24,19 @@ namespace grammar_parser {
2424
return std::make_pair(value, pos);
2525
}
2626

27-
uint32_t get_symbol_id(parse_state & state, const char * src, size_t len) {
27+
static uint32_t get_symbol_id(parse_state & state, const char * src, size_t len) {
2828
uint32_t next_id = static_cast<uint32_t>(state.symbol_ids.size());
2929
auto result = state.symbol_ids.insert(std::make_pair(std::string(src, len), next_id));
3030
return result.first->second;
3131
}
3232

33-
uint32_t generate_symbol_id(parse_state & state, const std::string & base_name) {
33+
static uint32_t generate_symbol_id(parse_state & state, const std::string & base_name) {
3434
uint32_t next_id = static_cast<uint32_t>(state.symbol_ids.size());
3535
state.symbol_ids[base_name + '_' + std::to_string(next_id)] = next_id;
3636
return next_id;
3737
}
3838

39-
void add_rule(
39+
static void add_rule(
4040
parse_state & state,
4141
uint32_t rule_id,
4242
const std::vector<llama_grammar_element> & rule) {
@@ -46,11 +46,11 @@ namespace grammar_parser {
4646
state.rules[rule_id] = rule;
4747
}
4848

49-
bool is_word_char(char c) {
49+
static bool is_word_char(char c) {
5050
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '-' || ('0' <= c && c <= '9');
5151
}
5252

53-
std::pair<uint32_t, const char *> parse_hex(const char * src, int size) {
53+
static std::pair<uint32_t, const char *> parse_hex(const char * src, int size) {
5454
const char * pos = src;
5555
const char * end = src + size;
5656
uint32_t value = 0;
@@ -73,7 +73,7 @@ namespace grammar_parser {
7373
return std::make_pair(value, pos);
7474
}
7575

76-
const char * parse_space(const char * src, bool newline_ok) {
76+
static const char * parse_space(const char * src, bool newline_ok) {
7777
const char * pos = src;
7878
while (*pos == ' ' || *pos == '\t' || *pos == '#' ||
7979
(newline_ok && (*pos == '\r' || *pos == '\n'))) {
@@ -88,7 +88,7 @@ namespace grammar_parser {
8888
return pos;
8989
}
9090

91-
const char * parse_name(const char * src) {
91+
static const char * parse_name(const char * src) {
9292
const char * pos = src;
9393
while (is_word_char(*pos)) {
9494
pos++;
@@ -99,7 +99,7 @@ namespace grammar_parser {
9999
return pos;
100100
}
101101

102-
std::pair<uint32_t, const char *> parse_char(const char * src) {
102+
static std::pair<uint32_t, const char *> parse_char(const char * src) {
103103
if (*src == '\\') {
104104
switch (src[1]) {
105105
case 'x': return parse_hex(src + 2, 2);
@@ -129,7 +129,7 @@ namespace grammar_parser {
129129
uint32_t rule_id,
130130
bool is_nested);
131131

132-
const char * parse_sequence(
132+
static const char * parse_sequence(
133133
parse_state & state,
134134
const char * src,
135135
const std::string & rule_name,
@@ -247,7 +247,7 @@ namespace grammar_parser {
247247
return pos;
248248
}
249249

250-
const char * parse_rule(parse_state & state, const char * src) {
250+
static const char * parse_rule(parse_state & state, const char * src) {
251251
const char * name_end = parse_name(src);
252252
const char * pos = parse_space(name_end, false);
253253
size_t name_len = name_end - src;
@@ -285,7 +285,7 @@ namespace grammar_parser {
285285
}
286286
}
287287

288-
void print_grammar_char(FILE * file, uint32_t c) {
288+
static void print_grammar_char(FILE * file, uint32_t c) {
289289
if (0x20 <= c && c <= 0x7f) {
290290
fprintf(file, "%c", static_cast<char>(c));
291291
} else {
@@ -294,7 +294,7 @@ namespace grammar_parser {
294294
}
295295
}
296296

297-
bool is_char_element(llama_grammar_element elem) {
297+
static bool is_char_element(llama_grammar_element elem) {
298298
switch (elem.type) {
299299
case LLAMA_GRETYPE_CHAR: return true;
300300
case LLAMA_GRETYPE_CHAR_NOT: return true;
@@ -304,7 +304,7 @@ namespace grammar_parser {
304304
}
305305
}
306306

307-
void print_rule_binary(FILE * file, const std::vector<llama_grammar_element> & rule) {
307+
static void print_rule_binary(FILE * file, const std::vector<llama_grammar_element> & rule) {
308308
for (auto elem : rule) {
309309
switch (elem.type) {
310310
case LLAMA_GRETYPE_END: fprintf(file, "END"); break;
@@ -334,7 +334,7 @@ namespace grammar_parser {
334334
fprintf(file, "\n");
335335
}
336336

337-
void print_rule(
337+
static void print_rule(
338338
FILE * file,
339339
uint32_t rule_id,
340340
const std::vector<llama_grammar_element> & rule,

0 commit comments

Comments
 (0)