================
@@ -216,6 +221,80 @@ static llvm::Error 
ExecuteIncrementalAction(CompilerInstance &CI,
   return llvm::Error::success();
 }
 
+// '#pragma clang repl optimize(<opt>)' sets the optimization level and size
+// used to emit modules for input parsed after the pragma. <opt> is an -O flag
+// spelled without the leading dash, e.g. O2 or Os.
+struct PragmaReplHandler : public PragmaHandler {
+  Interpreter &Interp;
+
+  PragmaReplHandler(Interpreter &Interp)
+      : PragmaHandler("repl"), Interp(Interp) {}
+
+  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
+                    Token &FirstToken) override {
+    Token Tok;
+    PP.LexUnexpandedToken(Tok);
+    if (Tok.isNot(tok::identifier) ||
+        !Tok.getIdentifierInfo()->isStr("optimize")) {
+      PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
+          << "clang repl";
+      return;
+    }
+
+    PP.LexUnexpandedToken(Tok);
+    if (Tok.isNot(tok::l_paren)) {
+      PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
+          << "clang repl optimize";
+      return;
+    }
+
+    // Route the flag through clang's own -O parsing so the pragma accepts
+    // exactly what the driver does.
+    PP.LexUnexpandedToken(Tok);
+    std::string Opt = PP.getSpelling(Tok);
+    std::string Flag = "-" + Opt;
+    const char *Argv[] = {Flag.c_str()};
+    unsigned MissingArgIndex, MissingArgCount;
+    llvm::opt::InputArgList Args = getDriverOptTable().ParseArgs(
+        Argv, MissingArgIndex, MissingArgCount,
+        llvm::opt::Visibility(options::CC1Option));
+    if (!Args.hasArg(options::OPT_O_Group)) {
+      PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_argument)
+          << Opt << "clang repl optimize" << /*Expected=*/true
+          << "an optimization flag such as 'O2' or 'Os'";
----------------
vgvassilev wrote:

We should enumerate the supported values.

https://github.com/llvm/llvm-project/pull/214793
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to