Skip to content

Commit

Permalink
[PP] error directive
Browse files Browse the repository at this point in the history
  • Loading branch information
LuisHsu committed Mar 23, 2024
1 parent 76c97e5 commit 2c1fa70
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/include/PreProcessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ struct PreProcessor {
void include_directive();
void undef_directive();
void line_directive();
void error_directive();
// void pragma_directive(PPToken& token); // TODO:
// void error_directive(PPToken& token); // TODO:
};

} // namespace WasmVM
Expand Down
15 changes: 14 additions & 1 deletion src/lib/PreProcessor/PreProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ PreProcessor::PPToken PreProcessor::get(){
}else if(direcitve_name == "line"){
line_directive();
}else if(direcitve_name == "error"){
// TODO:
error_directive();
}else if(direcitve_name == "pragma"){
// TODO:
}else if(direcitve_name == "include"){
Expand Down Expand Up @@ -365,3 +365,16 @@ void PreProcessor::line_directive(){
throw Exception::Exception("expected header name in #include");
}
}

void PreProcessor::error_directive(){
Line::iterator cur = skip_whitespace(line.begin(), line.end());
SourcePos pos = cur->value().pos;
std::stringstream message;
while(cur != line.end() && !cur->hold<TokenType::NewLine>()){
if(cur->has_value()){
message << cur->value();
}
cur = std::next(cur);
}
throw Exception::Error(pos, message.str());
}
1 change: 1 addition & 0 deletions test/pp_directive/error.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#error test error
4 changes: 4 additions & 0 deletions test/pp_directive/pp_directive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,8 @@ Suite pp_directive {
Expect(((TokenType::PPNumber)token.value()).sequence == "3");
pp.get();
})
Test("error", {
PreProcessor pp(std::filesystem::path("error.c"));
Throw(Exception::Exception, pp.get());
})
};

0 comments on commit 2c1fa70

Please sign in to comment.