1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-28 08:02:23 +01:00

codegen: Lambda improvements (#1476)

* Use #line directives in generated C++ code for lambdas

The #line directive in gcc is meant specifically for pieces of imported
code included in generated code, exactly what happens with lambdas in
the yaml files: https://gcc.gnu.org/onlinedocs/cpp/Line-Control.html

With this change, if I add the following at line 165 of kithen.yaml:
    - lambda: undefined_var == 5;

then "$ esphome kitchen.yaml compile" shows the following:

INFO Reading configuration kitchen.yaml...
INFO Generating C++ source...
INFO Compiling app...
INFO Running:  platformio run -d kitchen
<...>
Compiling .pioenvs/kitchen/src/main.cpp.o
kitchen.yaml: In lambda function:
kitchen.yaml:165:7: error: 'undefined_var' was not declared in this scope
*** [.pioenvs/kitchen/src/main.cpp.o] Error 1
== [FAILED] Took 2.37 seconds ==

* Silence gcc warning on multiline macros in lambdas

When the \ is used at the end of the C++ source in a lambda (line
continuation, often used in preprocessor macros), esphome will copy that
into main.cpp once as code and once as a // commment.  gcc will complain
about the multiline commment:

Compiling .pioenvs/kitchen/src/main.cpp.o
kitchen.yaml:640:3: warning: multi-line comment [-Wcomment]

Try to replace the \ with a "<cont>" for lack of a better idea.
This commit is contained in:
Andrew Zaborowski
2021-01-24 00:17:15 +01:00
committed by GitHub
parent 52c67d715b
commit c7c71089ce
4 changed files with 48 additions and 10 deletions

View File

@@ -11,7 +11,8 @@ import yaml.constructor
from esphome import core
from esphome.config_helpers import read_config_file
from esphome.core import EsphomeError, IPAddress, Lambda, MACAddress, TimePeriod, DocumentRange
from esphome.core import EsphomeError, IPAddress, Lambda, MACAddress, TimePeriod, \
DocumentRange, DocumentLocation
from esphome.helpers import add_class_to_obj
from esphome.util import OrderedDict, filter_yaml_files
@@ -30,9 +31,16 @@ class ESPHomeDataBase:
def esp_range(self):
return getattr(self, '_esp_range', None)
@property
def content_offset(self):
return getattr(self, '_content_offset', 0)
def from_node(self, node):
# pylint: disable=attribute-defined-outside-init
self._esp_range = DocumentRange.from_marks(node.start_mark, node.end_mark)
if isinstance(node, yaml.ScalarNode):
if node.style is not None and node.style in '|>':
self._content_offset = 1
class ESPForceValue:
@@ -257,7 +265,10 @@ class ESPHomeLoader(yaml.SafeLoader): # pylint: disable=too-many-ancestors
@_add_data_ref
def construct_lambda(self, node):
return Lambda(str(node.value))
start_mark = DocumentLocation.from_mark(node.start_mark)
if node.style is not None and node.style in '|>':
start_mark.line += 1
return Lambda(str(node.value), start_mark)
@_add_data_ref
def construct_force(self, node):