How best can I programmatically apply `__attribute__ ((unused))` to these auto-generated objects?

允我心安 提交于 2019-12-10 19:34:21

问题


In my makefile I have the following target, which "compiles" text/HTML resources into unsigned char arrays using xxd -i.

I wrap the result in an anonymous namespace and header guards for multiple-inclusion safety both inside and between translation units.

templates.h:
    @echo "#ifndef TEMPLATES_H" > templates.h
    @echo "#define TEMPLATES_H" >> templates.h
    @echo "// Auto-generated file! Do not modify!" >> templates.h
    @echo "// NB: arrays are not null-terminated" >> templates.h
    @echo "// (anonymous namespace used to force internal linkage)" >> templates.h
    @echo "namespace {" >> templates.h
    @echo "namespace templates {" >> templates.h
    @cd templates;\
    for i in * ;\
    do \
        echo "Compiling $$i...";\
        xxd -i $$i >> ../templates.h;\
    done;\
    cd ..
    @echo "}" >> templates.h
    @echo "}" >> templates.h
    @echo "#endif" >> templates.h

The output, if I had just one such resource, looks like this (actual content redacted):

#ifndef TEMPLATES_H
#define TEMPLATES_H
// Auto-generated file! Do not modify!
// NB: arrays are not null-terminated
// (anonymous namespace used to force internal linkage)
namespace {
namespace templates {
unsigned char alert_email_finished_events_html[] = {
  0x3c, 0x74, 0x61, 0x62, 0x6c, 0x0d, 0x0a
};
unsigned int alert_email_finished_events_html_len = 7;
}
}
#endif

What would be the best way to programmaticaly apply GCC's __attribute__ ((unused)) to these character arrays? I don't want GCC warning about any resources that I end up not using in any given TU, but I also don't want to turn "unused variable" warnings off altogether.


回答1:


I think a quick sed should work, given that the output of xxd -i is very regular:

xxd -i $$i | sed -e 's/ =/ __attribute__((unused)) =/' >> ../templates.h


来源:https://stackoverflow.com/questions/7660494/how-best-can-i-programmatically-apply-attribute-unused-to-these-auto-g

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!