changeset 205:b7591485f4cd before-12

fix MacroDefLength
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 07 Jun 2021 11:28:12 +0900
parents e348f3e5c8b2
children f17a3b42b08b dd44ba33042e
files clang/include/clang/Parse/Parser.h clang/lib/Lex/Preprocessor.cpp clang/lib/Parse/ParseCbC.cpp
diffstat 3 files changed, 30 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/clang/include/clang/Parse/Parser.h	Sat Jun 05 15:35:13 2021 +0900
+++ b/clang/include/clang/Parse/Parser.h	Mon Jun 07 11:28:12 2021 +0900
@@ -2070,6 +2070,7 @@
   StmtResult ParseGotoStatement();
 #ifndef noCbC
   StmtResult ParseCbCGotoStatement(ParsedAttributesWithRange &Attrs,StmtVector &Stmts);
+  void CompileFromString(const char *str, StmtVector &CompoundStmts);
 #endif
   StmtResult ParseContinueStatement();
   StmtResult ParseBreakStatement();
--- a/clang/lib/Lex/Preprocessor.cpp	Sat Jun 05 15:35:13 2021 +0900
+++ b/clang/lib/Lex/Preprocessor.cpp	Mon Jun 07 11:28:12 2021 +0900
@@ -1438,23 +1438,26 @@
 Token Preprocessor::ReadFromString(const char *src , SourceLocation Loc) {
     // Push the ( "string" ) tokens into the token stream.
     MacroInfo *MI = AllocateMacroInfo(Loc);
-    Token Tok,FirstTok,Result;
+    Token Tok;
     bool Invalid = false;
     std::unique_ptr<Lexer> lx(new Lexer(CurLexer->getFileID(),getSourceManager().getBuffer(CurLexer->getFileID(), Loc, &Invalid),*this));
     lx->InitLexer(src,src,src + strlen(src));
     lx->Lex(Tok);
-    FirstTok = Tok;
     CurLexer.swap(lx);
+    int i = 0;
     while (Tok.getKind() != tok::TokenKind::eof) {
       Tok.setLocation(Loc);
       MI->AddTokenToBody(Tok);
-      Lex(Tok);
+      Lex(Tok); i++;
     }
+    Tok.setLocation(Loc);
+    MI->AddTokenToBody(Tok); i++;
+    MI->DefinitionLength = i;
     CurLexer = std::move(lx);
     CurPPLexer = CurLexer.get();
-    EnterMacro(Result, Loc, MI , 0 );
-    CurTokenLexer->MacroExpansionStart = Loc;
-    return Result;
+    EnterMacro(Tok, Loc, MI , 0 );
+    CurTokenLexer->MacroDefLength = i;
+    return Tok;
 }
 
 #endif
--- a/clang/lib/Parse/ParseCbC.cpp	Sat Jun 05 15:35:13 2021 +0900
+++ b/clang/lib/Parse/ParseCbC.cpp	Mon Jun 07 11:28:12 2021 +0900
@@ -206,6 +206,25 @@
 ///         struct __CbC_env{
 ///           void *ret_p,*env;
 ///         }
+
+void Parser::CompileFromString(const char *str, StmtVector &CompoundStmts){
+  SourceLocation Loc = Tok.getLocation();
+  Token TokSave = Tok;
+  SourceLocation PLocSave = PrevTokLocation;
+  PP.ReadFromString(str,Loc); 
+  ConsumeAnyToken();
+  while(! Tok.is(tok::eof)) {
+      StmtVector Stmts;
+      StmtResult innerRes;
+      innerRes = ParseStatementOrDeclaration(Stmts,( ParsedStmtContext::Compound | ParsedStmtContext::AllowDeclarationsInC));
+      if (innerRes.isUsable())
+        CompoundStmts.push_back(innerRes.get());
+  }
+  ConsumeAnyToken();
+  Tok = TokSave ;
+  PrevTokLocation= PLocSave ;
+}
+
 ExprResult Parser::Prepare__envForGotoWithTheEnvExpr(){
 
   if (isVoidFunction()) { // error check : function type is void or not.
@@ -241,18 +260,7 @@
     CompoundStmts.push_back(innerRes.get());
 
   ConsumeAnyToken(); // eat the '__environment'.
-  Token TokSave = Tok;
-  SourceLocation PLocSave = PrevTokLocation;
-  // jmp_buf env_buf;   --> int env_buf[64];
-  PP.ReadFromString("int env_buf[64];",Loc);
-  ConsumeAnyToken();
-  // innerRes = CreateDeclStmt(bufII, false, false, 64, DeclSpec::TST_typename, CreateIdentifierInfo("int", Loc));
-  StmtVector Stmts;
-  innerRes = ParseStatementOrDeclaration(Stmts,( ParsedStmtContext::Compound | ParsedStmtContext::AllowDeclarationsInC));
-  Tok = TokSave ;
-  PrevTokLocation= PLocSave ;
-  if (innerRes.isUsable())
-    CompoundStmts.push_back(innerRes.get());
+  CompileFromString("int env_buf[64];",CompoundStmts); // 4*64 is enough for every arch right now
 
   // __CbC_environment.ret_p = &retval;
   innerRes = CreateAssignmentStmt(__CbC_envII, retvalII, true, true, ret_pII);