[all-commits] [llvm/llvm-project] dc4889: [clang-repl] Support statements on global scope in...

Vassil Vassilev via All-commits all-commits at lists.llvm.org
Fri Dec 2 23:18:42 PST 2022


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: dc4889357adb1b909b986b7d2f2a4b9a2cc40c8e
      https://github.com/llvm/llvm-project/commit/dc4889357adb1b909b986b7d2f2a4b9a2cc40c8e
  Author: Vassil Vassilev <v.g.vassilev at gmail.com>
  Date:   2022-12-03 (Sat, 03 Dec 2022)

  Changed paths:
    M clang/include/clang/AST/ASTNodeTraverser.h
    M clang/include/clang/AST/Decl.h
    M clang/include/clang/AST/RecursiveASTVisitor.h
    M clang/include/clang/Basic/DeclNodes.td
    M clang/include/clang/Basic/LangOptions.def
    M clang/include/clang/Driver/Options.td
    M clang/include/clang/Lex/Preprocessor.h
    M clang/include/clang/Parse/Parser.h
    M clang/include/clang/Sema/Sema.h
    M clang/include/clang/Sema/Template.h
    M clang/include/clang/Serialization/ASTBitCodes.h
    M clang/lib/AST/Decl.cpp
    M clang/lib/AST/DeclBase.cpp
    M clang/lib/AST/DeclPrinter.cpp
    M clang/lib/CodeGen/CGDecl.cpp
    M clang/lib/CodeGen/CodeGenModule.cpp
    M clang/lib/CodeGen/CodeGenModule.h
    M clang/lib/CodeGen/ModuleBuilder.cpp
    M clang/lib/Interpreter/IncrementalParser.cpp
    M clang/lib/Interpreter/Interpreter.cpp
    M clang/lib/Parse/ParseDecl.cpp
    M clang/lib/Parse/ParseTentative.cpp
    M clang/lib/Parse/Parser.cpp
    M clang/lib/Sema/SemaDecl.cpp
    M clang/lib/Serialization/ASTCommon.cpp
    M clang/lib/Serialization/ASTReaderDecl.cpp
    M clang/lib/Serialization/ASTWriterDecl.cpp
    A clang/test/Interpreter/disambiguate-decl-stmt.cpp
    A clang/test/Interpreter/execute-stmts.cpp
    A clang/test/Interpreter/stmt-serialization.cpp
    M clang/tools/libclang/CIndex.cpp
    M clang/unittests/Interpreter/InterpreterTest.cpp

  Log Message:
  -----------
  [clang-repl] Support statements on global scope in incremental mode.

This patch teaches clang to parse statements on the global scope to allow:
```
./bin/clang-repl
clang-repl> int i = 12;
clang-repl> ++i;
clang-repl> extern "C" int printf(const char*,...);
clang-repl> printf("%d\n", i);
13
clang-repl> %quit
```

Generally, disambiguating between statements and declarations is a non-trivial
task for a C++ parser. The challenge is to allow both standard C++ to be
translated as if this patch does not exist and in the cases where the user typed
a statement to be executed as if it were in a function body.

Clang's Parser does pretty well in disambiguating between declarations and
expressions. We have added DisambiguatingWithExpression flag which allows us to
preserve the existing and optimized behavior where needed and implement the
extra rules for disambiguating. Only few cases require additional attention:
  * Constructors/destructors -- Parser::isConstructorDeclarator was used in to
    disambiguate between ctor-looking declarations and statements on the global
    scope(eg. `Ns::f()`).
  * The template keyword -- the template keyword can appear in both declarations
    and statements. This patch considers the template keyword to be a declaration
    starter which breaks a few cases in incremental mode which will be tackled
    later.
  * The inline (and similar) keyword -- looking at the first token in many cases
    allows us to classify what is a declaration.
  * Other language keywords and specifiers -- ObjC/ObjC++/OpenCL/OpenMP rely on
    pragmas or special tokens which will be handled in subsequent patches.

The patch conceptually models a "top-level" statement into a TopLevelStmtDecl.
The TopLevelStmtDecl is lowered into a void function with no arguments.
We attach this function to the global initializer list to execute the statement
blocks in the correct order.

Differential revision: https://reviews.llvm.org/D127284




More information about the All-commits mailing list