@@ -44,6 +44,26 @@ struct {
44
44
};
45
45
} // end anonymous namespace
46
46
47
+ // Returns a pointer past the end of a valid macro name beginning at `Str`.
48
+ // Valid macro names match the regular expression [a-zA-Z_][0-9a-zA-Z_]*.
49
+ static const char *lexMacroName (const char *Str) {
50
+ assert (Str);
51
+
52
+ // Macro names start with [a-zA-Z_].
53
+ char First = *Str;
54
+ if (First != ' _' && !isalpha (First))
55
+ return Str;
56
+
57
+ // Eat the first character of the name.
58
+ ++Str;
59
+
60
+ // Match the rest of the identifier regex: [0-9a-zA-Z_]*
61
+ while (isalpha (*Str) || isdigit (*Str) || *Str == ' _' )
62
+ ++Str;
63
+
64
+ return Str;
65
+ }
66
+
47
67
TGLexer::TGLexer (SourceMgr &SM, ArrayRef<std::string> Macros) : SrcMgr(SM) {
48
68
CurBuffer = SrcMgr.getMainFileID ();
49
69
CurBuf = SrcMgr.getMemoryBuffer (CurBuffer)->getBuffer ();
@@ -54,9 +74,17 @@ TGLexer::TGLexer(SourceMgr &SM, ArrayRef<std::string> Macros) : SrcMgr(SM) {
54
74
PrepIncludeStack.push_back (
55
75
std::make_unique<std::vector<PreprocessorControlDesc>>());
56
76
57
- // Put all macros defined in the command line into the DefinedMacros set.
58
- for (const std::string &MacroName : Macros)
77
+ // Add all macros defined on the command line to the DefinedMacros set.
78
+ // Check invalid macro names and print fatal error if we find one.
79
+ for (const std::string &MacroName : Macros) {
80
+ const char *Begin = MacroName.c_str ();
81
+ const char *End = lexMacroName (Begin);
82
+ if (static_cast <size_t >(std::distance (Begin, End)) != MacroName.size ())
83
+ PrintFatalError (Twine (" Invalid macro name `" ) + Twine (MacroName) +
84
+ Twine (" ` specified on command line." ));
85
+
59
86
DefinedMacros.insert (MacroName);
87
+ }
60
88
}
61
89
62
90
SMLoc TGLexer::getLoc () const {
@@ -710,9 +738,8 @@ bool TGLexer::prepEatPreprocessorDirective(tgtok::TokKind Kind) {
710
738
return false ;
711
739
}
712
740
713
- tgtok::TokKind TGLexer::lexPreprocessor (
714
- tgtok::TokKind Kind, bool ReturnNextLiveToken) {
715
-
741
+ tgtok::TokKind TGLexer::lexPreprocessor (tgtok::TokKind Kind,
742
+ bool ReturnNextLiveToken) {
716
743
// We must be looking at a preprocessing directive. Eat it!
717
744
if (!prepEatPreprocessorDirective (Kind))
718
745
PrintFatalError (" lexPreprocessor() called for unknown "
@@ -912,14 +939,7 @@ StringRef TGLexer::prepLexMacroName() {
912
939
++CurPtr;
913
940
914
941
TokStart = CurPtr;
915
- // Macro names start with [a-zA-Z_].
916
- if (*CurPtr != ' _' && !isalpha (*CurPtr))
917
- return " " ;
918
-
919
- // Match the rest of the identifier regex: [0-9a-zA-Z_]*
920
- while (isalpha (*CurPtr) || isdigit (*CurPtr) || *CurPtr == ' _' )
921
- ++CurPtr;
922
-
942
+ CurPtr = lexMacroName (CurPtr);
923
943
return StringRef (TokStart, CurPtr - TokStart);
924
944
}
925
945
0 commit comments