返回资讯中心
外部精选
软件工程
#开发者实时流

Show HN: Asm Lens – See the assembly of each function in VS Code

See what your code compiles to without leaving the editor. Asm Lens puts the

Hacker News 最新idansoccer42026/11/10 · 7 天前30 分钟阅读

以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。

See what your code compiles to without leaving the editor. Asm Lens puts the instruction count of every function above it, opens the function's assembly next to your code, and links the two: hover a source line to see its instructions, hover an instruction to see its source line.

It compiles with Compiler Explorer (no setup) or with the compiler installed on your machine (nothing leaves it). C and C++ get the full treatment, including local headers, header-only code and templates. Rust, Go, Python, Zig, Swift, D and Fortran are supported too.

Instruction counts as CodeLens above every function: 12 instructions · GCC 15.2 · -O2. Template functions show every instantiation (3 instantiations · 4 / 6 / 9 instructions).

Assembly view next to the editor. Click a count to open it; it follows the cursor from function to function.

Two-way highlighting, like Compiler Explorer: each source line and its instructions share a color. Hover or click a source line to highlight its instructions; hover an instruction to highlight its source line (and the exact token, when the compiler reports a column). Everything else stays as it is (asmLens.dimOtherLines fades the rest instead). Hover a function's signature to preview its whole assembly.

Every function, including unused ones: methods defined inside a class, and static, inline and constexpr functions get their own counts even when nothing in the file calls them, with every compiler (asmLens.emitUnusedFunctions).

Headers work: analyze a .cpp and open the headers it includes, and they compile by themselves: every function, static inline or not, shows code even if the .cpp inlined it or never called it. A header that needs what the .cpp includes or defines before it gets those lines too.

Templates show code at once, with the types the code uses (Box<int>), else int. Click Instantiate… above a template to choose others (int, float, 4, std::vector<int>).

Robust against real projects: local headers are found and uploaded the way a compiler would find them; a missing header is replaced by an empty file; a function that does not compile is skipped and marked Not compiled, and the rest is analyzed.

Remote or local, per project or per compiler, on Windows, Linux and macOS. Local mode can take each file's compiler and flags from your build's compile_commands.json.

Any compiler on Compiler Explorer: GCC, Clang, MSVC (x64, x86, ARM64, ARM32), ICX/ICC, and the cross compilers for ARM, RISC-V, MIPS, PowerPC, AVR, MSP430, Z80, 6502 and more. One set of flags serves them all: -O2 -std=c++20 reaches MSVC as /O2 /std:c++20 /EHsc.

Fast, and visibly working: a progress notification shows each step (finding headers, compiling, retrying around errors) with a Cancel button; the status bar and the CodeLens spin while it runs. Unchanged code is answered from a cache, and Compiler Explorer is asked for compact replies.

Live while you edit: results stay on screen and move with the text; only the functions you change show Updating…, and the file is analyzed again in the background when you pause typing.

Quick start

Install the extension (or search for Asm Lens in the Extensions panel) and open a C, C++, Rust, Go, Python, … file.

Click Analyze with Asm Lens above any function (or run Asm Lens: Analyze Current File, or right-click in the editor).

Click a count to open the assembly view. Hover lines in either view.

While it compiles, a notification shows what is happening and lets you cancel. After that the counts follow your edits by themselves (see Editing).

You do not need to analyze again after a change:

Set asmLens.reanalyzeOn to save to compile only when you save, or never to compile only when you ask.

By default the file is compiled on godbolt.org with GCC 15.2 and -O2, in Intel syntax. Choose another compiler with the compiler name in the status bar (bottom right) or Asm Lens: Select Compiler. To change flags, include folders and so on for a project, run Asm Lens: Open or Create Project Config (see .asm-lens.json).

"mode": "remote" (default) "mode": "local"

Where it compiles A Compiler Explorer server: godbolt.org, or your own (baseUrl) On this machine

What leaves the machine The file, plus the local headers it includes Nothing

Compilers Any Compiler Explorer compiler: GCC, Clang, MSVC, ICX, … in many versions The compiler you name: any GCC- or Clang-compatible one for C/C++ (g++, clang++, g++-14, MinGW g++.exe, …)

Headers Found locally and uploaded with their folder layout Read from disk by the compiler; includePaths become -I

Standard library The server's Yours

Target What the server's compiler targets (mostly Linux x86-64) Your OS and CPU (x86-64, arm64; ELF, COFF or Mach-O)

Speed A network round trip (about 0.5–5 s); unchanged code is answered from a cache Usually well under a second

Works offline No Yes

Unsaved edits Used Used

Missing headers, failing functions Recovered (see below) Recovered the same way

Choose the mode in .asm-lens.json ("mode": "local"), per compiler ({ "id": "...", "mode": "local" }), or for every project with the asmLens.mode setting. The status bar shows which one is active (clang++ (local) or the Compiler Explorer compiler's name).

Local mode for C/C++ runs <compiler> <flags> -I<includePaths> -iquote <file folder> -S -g -o - -x c++ - with your unsaved text on standard input, then c++filt to demangle names. Asm Lens never runs your program and never builds the rest of the project.

Assembly syntax. Asm Lens shows Intel syntax and picks the flag for it itself, because the right flag depends on the compiler: on Compiler Explorer the server adds it per compiler; locally Asm Lens asks the compiler what it is (--version, -dumpmachine) and adds -masm=intel (GCC) or -mllvm -x86-asm-syntax=intel (Clang) for x86 targets only. A -masm=... in your flags is therefore not passed on (MSVC and non-x86 compilers reject it); write -masm=att to get AT&T syntax instead. If a compiler rejects any other flag (cc65: Unknown option: -O2), that flag is left out, the file is compiled again, and the output says so.

Flags for MSVC. Write your flags once, the GCC/Clang way. For MSVC and clang-cl, Asm Lens translates the common ones (the output lists each translation) and leaves the rest as written, so flags you write for MSVC (/O2 /std:c++latest) are kept as they are:

C++ also gets /EHsc, as Visual Studio projects do, unless you give an /EH option. The other way round, MSVC flags (/O2, /std:c++20, /W4, /DNAME) given to GCC or Clang are translated too.

Functions nothing calls. A compiler leaves out a function defined inside a class, or a static, inline or constexpr one, when nothing in the file uses it (and often when every call was inlined). To show its code anyway, the copy that is compiled marks such functions attribute((used)) on the same line (MSVC: a line after the end of the file takes each one's address, or calls a constructor). Your file is not changed, and if the marked copy does not compile, the file is compiled as written. Set asmLens.emitUnusedFunctions to false to see only what the compiler emits on its own.

Templates without being asked. A template has no code until it has types, so the copy that is compiled also instantiates each template, after the end of the file: with the types written where it is used, in the file or in an analyzed file that includes it (Box<long>), otherwise int (1 for a number). The CodeLens names the types (3 instructions · Box<long>). A class is compiled as a whole (template struct Box<long>;). If that fails, each member gets its own line (&Box<long>::get), so a member that does not compile for those types says so itself and costs the others nothing. An overloaded template is first asked for plainly (&Box<int>::get<int>, which is enough when SFINAE leaves one) and selected by its type only if that fails. Types you chose with Instantiate… replace these. asmLens.autoInstantiateTemplates turns this off.

Headers and the files that include them. Opening a header after analyzing a file that includes it compiles the header by itself, once for each saved version (not with "reanalyzeOn": "never"). A header with functions that lack code (compile errors, an #if that is off, templates without types) is also compiled the way the files including it see it: with the preprocessor lines before its #include, and with their entries in compile_commands.json, directly or through other headers. A file listed more than once (a 32-bit and a 64-bit build, in one database or several) is compiled with each entry, a .cpp as well. The best result is the main one, and the others fill in what it lacks, so a function behind -m32 or -DFEATURE_A and one behind -m64 or -DFEATURE_B can both show code. The CodeLens names the target (compile_commands.json -m32).

With "compileCommands" in .asm-lens.json (or the asmLens.compileCommands setting), each file is compiled the way your build compiles it: with the compiler and flags of its entry in the compile_commands.json database that CMake (-DCMAKE_EXPORT_COMPILE_COMMANDS=ON), Bear, Meson and others write. A header takes the entries of the files that include it, also through other headers. In remote mode the entry's defines, include folders, language standard and target options (-m...) are used, and the flags in .asm-lens.json come after them.

{ "mode": "local", "compileCommands": "build/compile_commands.json", // or several: ["build/a.json", "build/b.json"] "compilers": [{ "id": "local", "extraFlags": "-DASM_LENS" }] }

The entry's compiler runs in the entry's directory, with your editor text on standard input. The entry's flags are used without -c, -o <file>, -MD/-MMD/-MF/-MT/-MQ, -Werror*, -x <lang> and the source path; Asm Lens adds -S -g -o - -w -x c++ - (or -x c) and the syntax flag, and then the active compiler's extraFlags. Compiler launchers (ccache, sccache, distcc) are skipped.

A file without its own entry (a header, a new file) uses the entry of a source file that includes it (one you analyzed, else one found by searching the nearest folders first), else an entry in the same or the nearest folder. Without any database entry, the usual flags, defines and includePaths apply.

The database is read again whenever it changes; Asm Lens: Reload Compile Commands forces it. The output shows the final command and which entry was used.

Local mode needs a GCC- or Clang-compatible compiler; an MSVC (cl.exe, clang-cl) database is reported, and remote mode with an MSVC compiler from Compiler Explorer is the way to see its code.

Headers and templates

Analyzing a header on its own. A header usually compiles to nothing: an inline or static inline function that nothing calls is dropped, and a template is just a pattern. When you analyze a header (.h, .hpp, .hxx, .hh, .inl, .ipp, .tpp, …), Asm Lens marks every non-template function in it attribute((used)) (only in the copy that is compiled, keeping every line in place), so each one is emitted and gets a count: free functions, static inline functions, and member functions defined inside classes. If some functions cannot compile on their own (they need another header or a define), only those say Not compiled; the rest keep their counts.

Templates. Above every template function, and every member function of a class template, there is an Instantiate… CodeLens. Click it and type the template arguments:

If a class does not compile as a whole for the types you chose (one member calls v.size() and you typed int), only the member you clicked is compiled, and Asm Lens tells you so.

The requests are added after the file's last line, so line numbers do not change. They are remembered per file (shown in a CodeLens at the top of the file; click it to remove some) and compiled every time you analyze that file. A request that does not compile (a misspelled type, for example) is removed and the compiler's message is shown. Template constructors cannot be instantiated this way; use them from a .cpp file instead.

Analyzing a .cpp an

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →