[PATCH] D12216: Cleaning up LLVM IR mode for Emacs

Wilfred Hughes via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 9 14:57:56 PDT 2015


Wilfred added inline comments.

================
Comment at: utils/emacs/llvm-mode.el:11
@@ -10,2 +10,3 @@
 
-(defvar llvm-mode-syntax-table nil
+(defvar llvm-mode-syntax-table
+  (let ((table (make-syntax-table)))
----------------
artagnon wrote:
> I _think_ this is okay.
Let me try to put your mind at ease :)

We started with:

```
(defvar llvm-mode-syntax-table nil
  "Syntax table used while in LLVM mode.")
(if (not llvm-mode-syntax-table)
    (progn
      (setq llvm-mode-syntax-table (make-syntax-table))
      (mapc (function (lambda (n)
                        (modify-syntax-entry (aref n 0)
                                             (aref n 1)
                                             llvm-mode-syntax-table)))
            '(
              ;; whitespace (` ')
              [?\^m " "]
              [?\f  " "]
              [?\n  " "]
              [?\t  " "]
              [?\   " "]
              ;; word constituents (`w')
              ;;[?<  "w"]
              ;;[?>  "w"]
              [?%  "w"]
              ;;[?_  "w  "]
              ;; comments
              [?\;  "< "]
              [?\n  "> "]
              ;;[?\r  "> "]
              ;;[?\^m "> "]
              ;; symbol constituents (`_')
              ;; punctuation (`.')
              ;; open paren (`(')
              ;; close paren (`)')
              ;; string quote ('"')
              [?\" "\""]))))
```

This style of defining a variable and setting it afterwards is intended to make it easier to repeatedly evaluate the expression when developing the code. However, if you use `edebug-eval-defun`, your expression is re-evaluated anyway, allowing you to use `defvar` directly:

```
(defvar llvm-mode-syntax-table
  (progn
    (setq llvm-mode-syntax-table (make-syntax-table))
    (mapc (function (lambda (n)
                      (modify-syntax-entry (aref n 0)
                                           (aref n 1)
                                           llvm-mode-syntax-table)))
          '(
            ;; whitespace (` ')
            [?\^m " "]
            [?\f  " "]
            [?\n  " "]
            [?\t  " "]
            [?\   " "]
            ;; word constituents (`w')
            ;;[?<  "w"]
            ;;[?>  "w"]
            [?%  "w"]
            ;;[?_  "w  "]
            ;; comments
            [?\;  "< "]
            [?\n  "> "]
            ;;[?\r  "> "]
            ;;[?\^m "> "]
            ;; symbol constituents (`_')
            ;; punctuation (`.')
            ;; open paren (`(')
            ;; close paren (`)')
            ;; string quote ('"')
            [?\" "\""]))
    llvm-mode-syntax-table)
  "Syntax table used while in LLVM mode.")
```

Remove comments and commented-out expressions:

```
(defvar llvm-mode-syntax-table
  (progn
    (setq llvm-mode-syntax-table (make-syntax-table))
    (mapc (function (lambda (n)
                      (modify-syntax-entry (aref n 0)
                                           (aref n 1)
                                           llvm-mode-syntax-table)))
          '([?\^m " "]
            [?\f  " "]
            [?\n  " "]
            [?\t  " "]
            [?\   " "]
            [?%  "w"]
            [?\;  "< "]
            [?\n  "> "]
            [?\" "\""]))
    llvm-mode-syntax-table)
  "Syntax table used while in LLVM mode.")
```

Just call the function directly instead of using map:

```
(defvar llvm-mode-syntax-table
  (let ((table (make-syntax-table)))
    (modify-syntax-entry ?\^m " " table)
    (modify-syntax-entry ?\f  " " table)
    (modify-syntax-entry ?\n  " " table)
    (modify-syntax-entry ?\t  " " table)
    (modify-syntax-entry ?\   " " table)
    (modify-syntax-entry ?%  "w" table)
    (modify-syntax-entry ?\;  "< " table)
    (modify-syntax-entry ?\n  "> " table)
    (modify-syntax-entry ?\" "\"" table)
    table)
  "Syntax table used while in LLVM mode.")
```

Whitespace characters are treated as whitespace syntax by
default. Similarly, the default syntax table treats `"` as a string
delimiter by default. We can thus reduce our syntax table:

```
(defvar llvm-mode-syntax-table
  (let ((table (make-syntax-table)))
    (modify-syntax-entry ?%  "w" table)
    (modify-syntax-entry ?\;  "< " table)
    (modify-syntax-entry ?\n  "> " table)
    table)
  "Syntax table used while in LLVM mode.")
```

It's good practice to only treat `[a-zA-Z0-9]` as word constituents in Emacs. Given a variable `foo_bar_baz`, it's better to treat `_` as a symbol constituent so users can choose to move by word (foo to bar) or by symbol as they wish. We change `%` to be a symbol constituent:

```
(defvar llvm-mode-syntax-table
  (let ((table (make-syntax-table)))
    (modify-syntax-entry ?%  "_" table)
    (modify-syntax-entry ?\;  "< " table)
    (modify-syntax-entry ?\n  "> " table)
    table)
  "Syntax table used while in LLVM mode.")
```

Finally, since `.` can occur in function names (e.g. `llvm.memset.p0i8.i32`), treat that as a symbol constituent, so we highlight such functions correctly.

```
(defvar llvm-mode-syntax-table
  (let ((table (make-syntax-table)))
    (modify-syntax-entry ?%  "_" table)
    (modify-syntax-entry ?. "_" table table)
    (modify-syntax-entry ?\;  "< " table)
    (modify-syntax-entry ?\n  "> " table)
    table)
  "Syntax table used while in LLVM mode.")
```


http://reviews.llvm.org/D12216





More information about the llvm-commits mailing list