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