Skip to content

Commit 9d5b07f

Browse files
authored
fix(preproc): Fix macro redefinition
1 parent 4ae48c3 commit 9d5b07f

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

src/cxx/preprocessor.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ struct Preprocessor::Private {
335335
Control *control_ = nullptr;
336336
DiagnosticsClient *diagnosticsClient_ = nullptr;
337337
CommentHandler *commentHandler_ = nullptr;
338+
PreprocessorDelegate *delegate_ = nullptr;
338339
bool canResolveFiles_ = true;
339340
std::vector<fs::path> systemIncludePaths_;
340341
std::vector<fs::path> quoteIncludePaths_;
@@ -1322,6 +1323,11 @@ void Preprocessor::Private::defineMacro(const TokList *ts) {
13221323

13231324
auto name = ts->head->text;
13241325

1326+
if (auto it = macros_.find(name); it != macros_.end()) {
1327+
warning(ts->head->token(), "'{}' macro redefined", name);
1328+
macros_.erase(it);
1329+
}
1330+
13251331
if (ts->tail && !ts->tail->head->space &&
13261332
ts->tail->head->is(TokenKind::T_LPAREN)) {
13271333
ts = ts->tail->tail; // skip macro name and '('
@@ -1464,6 +1470,12 @@ void Preprocessor::setCommentHandler(CommentHandler *commentHandler) {
14641470
d->commentHandler_ = commentHandler;
14651471
}
14661472

1473+
PreprocessorDelegate *Preprocessor::delegate() const { return d->delegate_; }
1474+
1475+
void Preprocessor::setDelegate(PreprocessorDelegate *delegate) {
1476+
d->delegate_ = delegate;
1477+
}
1478+
14671479
bool Preprocessor::canResolveFiles() const { return d->canResolveFiles_; }
14681480

14691481
void Preprocessor::setCanResolveFiles(bool canResolveFiles) {

src/cxx/preprocessor.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Token;
3535
class DiagnosticsClient;
3636
class CommentHandler;
3737
class Preprocessor;
38+
class PreprocessorDelegate;
3839

3940
class CommentHandler {
4041
public:
@@ -44,6 +45,11 @@ class CommentHandler {
4445
const Token &token) = 0;
4546
};
4647

48+
class PreprocessorDelegate {
49+
public:
50+
virtual ~PreprocessorDelegate() = default;
51+
};
52+
4753
class Preprocessor {
4854
public:
4955
Preprocessor(Preprocessor &&) noexcept = default;
@@ -57,6 +63,9 @@ class Preprocessor {
5763
CommentHandler *commentHandler() const;
5864
void setCommentHandler(CommentHandler *commentHandler);
5965

66+
PreprocessorDelegate *delegate() const;
67+
void setDelegate(PreprocessorDelegate *delegate);
68+
6069
bool canResolveFiles() const;
6170
void setCanResolveFiles(bool canResolveFiles);
6271

src/frontend/frontend.cc

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ struct VerifyDiagnostics : DiagnosticsClient {
9696
std::list<Diagnostic> reportedDiagnostics;
9797
bool verify = false;
9898

99+
bool hasErrors() const {
100+
if (verify) return !reportedDiagnostics.empty();
101+
102+
for (const auto& d : reportedDiagnostics) {
103+
if (d.severity() == Severity::Error || d.severity() == Severity::Fatal)
104+
return true;
105+
}
106+
107+
return false;
108+
}
109+
99110
void report(const Diagnostic& diagnostic) override {
100111
if (!verify) {
101112
DiagnosticsClient::report(diagnostic);
@@ -303,7 +314,7 @@ bool runOnFile(const CLI& cli, const std::string& fileName) {
303314
diagnosticsClient.verifyExpectedDiagnostics(
304315
verifyCommentHandler.expectedDiagnostics);
305316

306-
return true;
317+
return !diagnosticsClient.hasErrors();
307318
}
308319

309320
preprocesor->squeeze();
@@ -330,7 +341,7 @@ bool runOnFile(const CLI& cli, const std::string& fileName) {
330341
diagnosticsClient.verifyExpectedDiagnostics(
331342
verifyCommentHandler.expectedDiagnostics);
332343

333-
return result;
344+
return !diagnosticsClient.hasErrors();
334345
}
335346

336347
} // namespace cxx
@@ -358,9 +369,13 @@ int main(int argc, char* argv[]) {
358369
return EXIT_FAILURE;
359370
}
360371

372+
int existStatus = EXIT_SUCCESS;
373+
361374
for (const auto& fileName : inputFiles) {
362-
runOnFile(cli, fileName);
375+
if (!runOnFile(cli, fileName)) {
376+
existStatus = EXIT_FAILURE;
377+
}
363378
}
364379

365-
return EXIT_SUCCESS;
380+
return existStatus;
366381
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %cxx -verify -E %s -o - | filecheck %s
2+
3+
#define RESULT 0
4+
5+
#define RESULT 1 // expected-warning {{'RESULT' macro redefined}}
6+
7+
int main() {
8+
int result = RESULT;
9+
return result;
10+
}
11+
12+
// CHECK: int result = 1;

0 commit comments

Comments
 (0)