-
Notifications
You must be signed in to change notification settings - Fork 4
/
NacroPragmaHandler.cpp
64 lines (53 loc) · 1.69 KB
/
NacroPragmaHandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "clang/Basic/TokenKinds.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/PPCallbacks.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/raw_ostream.h"
#include "NacroParsers.h"
#include "NacroExpanders.h"
#include "NacroVerifier.h"
#include <memory>
using namespace clang;
namespace {
struct NacroPragmaHandler : public PragmaHandler {
NacroPragmaHandler() : PragmaHandler("nacro") {}
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &PragmaTok) override;
};
void NacroPragmaHandler::HandlePragma(Preprocessor &PP,
PragmaIntroducer Introducer,
Token &PragmaTok) {
Token Tok;
Tok.startToken();
// Decide the category
// (Currently we have only one category: rule)
StringRef Category;
SmallVector<Token, 1> PragmaArgs;
PP.Lex(Tok);
if(Tok.isNot(tok::identifier)) {
llvm::errs() << "Expecting nacro category\n";
return;
}
auto* II = Tok.getIdentifierInfo();
assert(II);
Category = II->getName();
do {
PP.Lex(Tok);
PragmaArgs.push_back(Tok);
} while(Tok.isNot(tok::eod));
if(Category == "rule") {
NacroRuleParser Parser(PP, PragmaArgs);
if(!Parser.Parse()) return;
auto* Rule = Parser.getNacroRule();
NacroRuleExpander Expander(Rule, PP);
if(Expander.Expand()) return;
// FIXME: Make AddNacroRule completely static
NacroVerifier(PP.getSourceManager())
.AddNacroRule(Expander.getNacroRule());
} else {
llvm::errs() << "Unrecognized category: "
<< Category << "\n";
}
}
PragmaHandlerRegistry::Add<NacroPragmaHandler> X("nacro", "New Macro");
} // end anonymous namespace