xref: /aosp_15_r20/external/pigweed/docs/showcases/sense/tutorial/code_intelligence.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1.. _showcase-sense-tutorial-intel:
2
3================================
44. Explore C++ code intelligence
5================================
6Sense's integration with :ref:`module-pw_ide` enables fast and accurate
7code navigation, autocompletion based on a deep understanding of the
8code structure, and instant compiler warnings and errors. Try the
9code navigation now:
10
11.. _Command Palette: https://code.visualstudio.com/docs/getstarted/userinterface#_command-palette
12
13.. tab-set::
14
15   .. tab-item:: VS Code
16      :sync: vsc
17
18      #. Press :kbd:`Control+Shift+P` (:kbd:`Command+Shift+P` on macOS) to open
19         the `Command Palette`_.
20
21      #. Start typing ``Pigweed: Select Code Analysis Target`` and press
22         :kbd:`Enter` to start executing that command.
23
24         .. figure:: https://storage.googleapis.com/pigweed-media/sense/20240802/target.png
25
26         .. tip::
27
28            You can also select code analysis targets from the bottom-left
29            of your VS Code GUI, in the status bar. In the next image,
30            the mouse cursor is hovering over the GUI element for selecting
31            code analysis targets.
32
33            .. figure:: https://storage.googleapis.com/pigweed-media/sense/20240802/select_target_status_bar.png
34
35      #. Select the ``rp2040`` option.
36
37         .. tip::
38
39            In the status bar (the bar at the bottom of VS Code) you may see
40            informational messages about clang indexing. You don't need to wait
41            for that indexing to complete.
42
43         The code intelligence is now set up to help you with physical Pico
44         programming. If you had selected the other option, ``host_simulator``,
45         the code intelligence would be set up to help with programming the
46         simulated app that you will run on your development host in
47         :ref:`showcase-sense-tutorial-sim`. Verify the platform-specific
48         code intelligence now by making sure that :ref:`module-pw_log` invocations
49         resolve to different backends.
50
51      #. Open ``//apps/blinky/main.cc``.
52
53      #. Right-click the ``PW_LOG_INFO()`` invocation and select
54         **Go to Definition**.
55
56         .. figure:: https://storage.googleapis.com/pigweed-media/sense/20240802/go_to_definition.png
57            :alt: Selecting "Go to Definition" after right-clicking PW_LOG_INFO()
58
59         This should take you to a file called ``log.h``:
60
61         .. code-block:: c++
62
63            #ifndef PW_LOG_INFO
64            #define PW_LOG_INFO(...) \  // Your cursor should be on this line.
65              PW_LOG(PW_LOG_LEVEL_INFO, PW_LOG_MODULE_NAME, PW_LOG_FLAGS, __VA_ARGS__)
66            #endif  // PW_LOG_INFO
67
68      #. Right-click the ``PW_LOG()`` invocation (on the line below your cursor)
69         and select **Go to Definition** again. You should jump here:
70
71         .. code-block:: c++
72
73            #ifndef PW_LOG
74            #define PW_LOG(  // Your cursor should be on this line.
75              // ...
76                  PW_HANDLE_LOG(level, module, flags, __VA_ARGS__);
77              // ...
78
79
80      #. Finally, right-click the ``PW_HANDLE_LOG()`` invocation
81         and select **Go to Definition** one last time. You should jump to
82         a :ref:`module-pw_log_tokenized` backend header. You can hover
83         over the filename in the tab (``log_backend.h``) to see the full
84         path to the header.
85
86      #. Open the Command Palette, switch your target to ``host_simulator``,
87         and then repeat this **Go to Definition** workflow again, starting from
88         ``//apps/blinky/main.cc``. You should see the definitions finally
89         resolve to a :ref:`module-pw_log_string` backend header.
90
91         This proves that code intelligence is working because the original
92         call to ``PW_LOG_INFO()`` in ``//apps/blinky/main.cc`` is basically
93         a generic API that gets resolved at compile-time. The resolution
94         depends on what platform you're building for (``rp2040`` or
95         ``host_simulator``). See :ref:`docs-facades`.
96
97      Here's a diagram summary of how the code intelligence resolved to
98      different files depending on the code analysis target you selected:
99
100      .. mermaid::
101
102         flowchart LR
103
104           a["main.cc"] --> b["log.h"]
105           b["log.h"] -. rp2040 .-> c["pw_log_tokenized/.../log_backend.h"]
106           b["log.h"] -. host_simulator .-> d["pw_log_string/.../log_backend.h"]
107
108   .. tab-item:: CLI
109      :sync: cli
110
111      This feature is only supported in VS Code.
112
113.. _showcase-sense-tutorial-intel-summary:
114
115-------
116Summary
117-------
118Portable, hardware-agnostic software abstractions such as :ref:`module-pw_log`
119make it easier to reuse code across projects and hardware platforms. But they
120also make it more difficult to correctly navigate references in your codebase.
121The Pigweed extension for VS Code can solve this problem; you just need to
122tell it what hardware platform within your codebase it should focus on.
123
124Next, head over to :ref:`showcase-sense-tutorial-hosttests` to learn how to run
125unit tests on your development host so that you can verify that Sense's logic is
126correct before attempting to run it.
127