Skip to content

Commit

Permalink
Merge pull request #4024 from masatake/make--extract-D
Browse files Browse the repository at this point in the history
Make: add CppDef extra for extracting FOO in -DFOO as a macro of CPreProcessor

See bootlin/elixir#221
  • Loading branch information
masatake authored Jul 11, 2024
2 parents c6dbafc + c16adc6 commit e79e67d
Show file tree
Hide file tree
Showing 16 changed files with 617 additions and 15 deletions.
5 changes: 5 additions & 0 deletions Tmain/extras-long.d/stdout-expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ s subparser yes NONE no Include tags generated by
- implicitClass no GDScript no Include tag for the implicitly defined unnamed class
- doubleSharps no IPythonCell no Include cells starting from ##
- configPrefixed yes Kconfig no prepend CONFIG_ to config names
- CppDef no Make no Include FOO in -DFOO as as a name of CPreProcessor macro
- guessedFromFileName yes PkgConfig no the guessed package name of the .pc file
- funcmap yes QemuHX no Include mapping SQMP to C function name
- whitespaceSwapped yes Robot no Include tags swapping whitespace and underscore chars
Expand All @@ -29,6 +30,7 @@ s subparser yes NONE no Include tags generated by
- implicitClass no GDScript no Include tag for the implicitly defined unnamed class
- doubleSharps no IPythonCell no Include cells starting from ##
- configPrefixed yes Kconfig no prepend CONFIG_ to config names
- CppDef no Make no Include FOO in -DFOO as as a name of CPreProcessor macro
- guessedFromFileName yes PkgConfig no the guessed package name of the .pc file
- funcmap yes QemuHX no Include mapping SQMP to C function name
- whitespaceSwapped yes Robot no Include tags swapping whitespace and underscore chars
Expand All @@ -46,6 +48,7 @@ s subparser yes NONE no Include tags generated by
- implicitClass no GDScript no Include tag for the implicitly defined unnamed class
- doubleSharps no IPythonCell no Include cells starting from ##
- configPrefixed yes Kconfig no prepend CONFIG_ to config names
- CppDef no Make no Include FOO in -DFOO as as a name of CPreProcessor macro
- guessedFromFileName yes PkgConfig no the guessed package name of the .pc file
- funcmap yes QemuHX no Include mapping SQMP to C function name
- whitespaceSwapped yes Robot no Include tags swapping whitespace and underscore chars
Expand All @@ -63,6 +66,7 @@ s subparser yes NONE no Include tags generated by
- implicitClass no GDScript no Include tag for the implicitly defined unnamed class
- doubleSharps no IPythonCell no Include cells starting from ##
- configPrefixed yes Kconfig no prepend CONFIG_ to config names
- CppDef no Make no Include FOO in -DFOO as as a name of CPreProcessor macro
- guessedFromFileName yes PkgConfig no the guessed package name of the .pc file
- funcmap yes QemuHX no Include mapping SQMP to C function name
- whitespaceSwapped yes Robot no Include tags swapping whitespace and underscore chars
Expand All @@ -80,6 +84,7 @@ s subparser yes NONE no Include tags generated by
- implicitClass no GDScript no Include tag for the implicitly defined unnamed class
- doubleSharps no IPythonCell no Include cells starting from ##
- configPrefixed yes Kconfig no prepend CONFIG_ to config names
- CppDef no Make no Include FOO in -DFOO as as a name of CPreProcessor macro
- guessedFromFileName yes PkgConfig no the guessed package name of the .pc file
- funcmap yes QemuHX no Include mapping SQMP to C function name
- whitespaceSwapped yes Robot no Include tags swapping whitespace and underscore chars
2 changes: 2 additions & 0 deletions Tmain/list-extras.d/stdout-expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ s subparser yes NONE no Include tags generated by
- implicitClass no GDScript no Include tag for the implicitly defined unnamed class
- doubleSharps no IPythonCell no Include cells starting from ##
- configPrefixed yes Kconfig no prepend CONFIG_ to config names
- CppDef no Make no Include FOO in -DFOO as as a name of CPreProcessor macro
- guessedFromFileName yes PkgConfig no the guessed package name of the .pc file
- funcmap yes QemuHX no Include mapping SQMP to C function name
- whitespaceSwapped yes Robot no Include tags swapping whitespace and underscore chars
Expand All @@ -29,6 +30,7 @@ s subparser yes NONE no Include tags generated by subparsers
- implicitClass no GDScript no Include tag for the implicitly defined unnamed class
- doubleSharps no IPythonCell no Include cells starting from ##
- configPrefixed yes Kconfig no prepend CONFIG_ to config names
- CppDef no Make no Include FOO in -DFOO as as a name of CPreProcessor macro
- guessedFromFileName yes PkgConfig no the guessed package name of the .pc file
- funcmap yes QemuHX no Include mapping SQMP to C function name
- whitespaceSwapped yes Robot no Include tags swapping whitespace and underscore chars
Expand Down
3 changes: 3 additions & 0 deletions Units/parser-make.r/cpp-marcros.d/args.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--sort=no
--fields=+{language}{signature}
--extras-Make=+{CppDef}
57 changes: 57 additions & 0 deletions Units/parser-make.r/cpp-marcros.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
REALMODE_CFLAGS input.mak /^REALMODE_CFLAGS := -std=gnu11 -m16 -g -Os -DDISABLE_BRANCH_PROFILING -D__DISABLE_EXPORTS \\$/;" m language:Make
DISABLE_BRANCH_PROFILING input.mak /^REALMODE_CFLAGS := -std=gnu11 -m16 -g -Os -DDISABLE_BRANCH_PROFILING -D__DISABLE_EXPORTS \\$/;" d language:CPreProcessor
__DISABLE_EXPORTS input.mak /^REALMODE_CFLAGS := -std=gnu11 -m16 -g -Os -DDISABLE_BRANCH_PROFILING -D__DISABLE_EXPORTS \\$/;" d language:CPreProcessor
MYDEF input.mak /^ -DMYDEF=1 \\$/;" d language:CPreProcessor
MYDEF0 input.mak /^ -DMYDEF0 \\$/;" d language:CPreProcessor
MYDEF1 input.mak /^ -DMYDEF1=1 \\$/;" d language:CPreProcessor
MYDEF2 input.mak /^ -DMYDEF2(X)=X \\$/;" d language:CPreProcessor signature:(X)
MYDEF3 input.mak /^ -D MYDEF3 \\$/;" d language:CPreProcessor
MYDEF4 input.mak /^ -D MYDEF4=1 \\$/;" d language:CPreProcessor
MYDEF5 input.mak /^ -D MYDEF5(X)=X \\$/;" d language:CPreProcessor signature:(X)
MYDEF6 input.mak /^ -D'MYDEF6' \\$/;" d language:CPreProcessor
MYDEF7 input.mak /^ -D'MYDEF7=1' \\$/;" d language:CPreProcessor
MYDEF8 input.mak /^ -D'MYDEF8(X)=X' \\$/;" d language:CPreProcessor signature:(X)
MYDEF9 input.mak /^ -D 'MYDEF9' \\$/;" d language:CPreProcessor
MYDEFa input.mak /^ -D 'MYDEFa=1' \\$/;" d language:CPreProcessor
MYDEFb input.mak /^ -D 'MYDEFb(X)=X' \\$/;" d language:CPreProcessor signature:(X)
MYDEFc input.mak /^ -D"MYDEFc" \\$/;" d language:CPreProcessor
MYDEFd input.mak /^ -D"MYDEFd=1" \\$/;" d language:CPreProcessor
MYDEFe input.mak /^ -D"MYDEFe(X)=X" \\$/;" d language:CPreProcessor signature:(X)
MYDEFf input.mak /^ -D "MYDEFf" \\$/;" d language:CPreProcessor
MYDEFg input.mak /^ -D "MYDEFg=1" \\$/;" d language:CPreProcessor
MYDEFh input.mak /^ -D "MYDEFh(X)=X" \\$/;" d language:CPreProcessor signature:(X)
KBUILD_CPPFLAGS input.mak /^KBUILD_CPPFLAGS := -D__KERNEL__$/;" m language:Make
__KERNEL__ input.mak /^KBUILD_CPPFLAGS := -D__KERNEL__$/;" d language:CPreProcessor
KBUILD_RUSTFLAGS input.mak /^KBUILD_RUSTFLAGS := $(rust_common_flags) \\$/;" m language:Make
clippy::float_arithmetic input.mak /^ -Dclippy::float_arithmetic$/;" d language:CPreProcessor
KBUILD_AFLAGS_KERNEL input.mak /^KBUILD_AFLAGS_KERNEL :=$/;" m language:Make
KBUILD_CFLAGS_KERNEL input.mak /^KBUILD_CFLAGS_KERNEL :=$/;" m language:Make
KBUILD_RUSTFLAGS_KERNEL input.mak /^KBUILD_RUSTFLAGS_KERNEL :=$/;" m language:Make
KBUILD_AFLAGS_MODULE input.mak /^KBUILD_AFLAGS_MODULE := -DMODULE$/;" m language:Make
MODULE input.mak /^KBUILD_AFLAGS_MODULE := -DMODULE$/;" d language:CPreProcessor
KBUILD_CFLAGS_MODULE input.mak /^KBUILD_CFLAGS_MODULE := -DMODULE$/;" m language:Make
MODULE input.mak /^KBUILD_CFLAGS_MODULE := -DMODULE$/;" d language:CPreProcessor
KBUILD_RUSTFLAGS_MODULE input.mak /^KBUILD_RUSTFLAGS_MODULE := --cfg MODULE$/;" m language:Make
all input.mak /^all:$/;" t language:Make
FOO input.mak /^ $(CC) -DFOO ...$/;" d language:CPreProcessor
macdef input.mak /^define macdef$/;" m language:Make
MACDEF_CPPFLAGS input.mak /^ MACDEF_CPPFLAGS = -DMACDEFMAC0(X0,Y0,Z0)$/;" m language:Make macro:macdef
MACDEFMAC0 input.mak /^ MACDEF_CPPFLAGS = -DMACDEFMAC0(X0,Y0,Z0)$/;" d language:CPreProcessor signature:(X0,Y0,Z0)
MACDEFMAC1 input.mak /^ MACDEF_CPPFLAGS += -DMACDEFMAC1(X1,Y1,Z1)=X1##Y1##Z1$/;" d language:CPreProcessor signature:(X1,Y1,Z1)
MACDEF_CPPFLAGS_BROKEN0 input.mak /^ MACDEF_CPPFLAGS_BROKEN0 = -DMACDEFMAC_BROKEN0(A0,B0,C0$/;" m language:Make macro:macdef
MACDEFMAC_BROKEN0 input.mak /^ MACDEF_CPPFLAGS_BROKEN0 = -DMACDEFMAC_BROKEN0(A0,B0,C0$/;" d language:CPreProcessor
MACDEF_CPPFLAGS_BROKEN1 input.mak /^ MACDEF_CPPFLAGS_BROKEN1 = -D MACDEFMAC_BROKEN1(A1,B1,C1$/;" m language:Make macro:macdef
MACDEFMAC_BROKEN1 input.mak /^ MACDEF_CPPFLAGS_BROKEN1 = -D MACDEFMAC_BROKEN1(A1,B1,C1$/;" d language:CPreProcessor
MACDEFMAC2 input.mak /^ MACDEF_CPPFLAGS += -DMACDEFMAC2(X2,Y2,Z2)="f b"$/;" d language:CPreProcessor signature:(X2,Y2,Z2)
install-man input-0.mak /^install-man: $(srctree)\/man\/pfrut.8$/;" t language:Make
install input-0.mak /^install:$/;" t language:Make
ACPI_DEBUGGER0 input-1.mak /^CFLAGS0 += -DACPI_DEBUGGER0\\$/;" d language:CPreProcessor
CFLAGS1 input-1.mak /^CFLAGS1 ?= -DACPI_DEBUGGER1\\$/;" m language:Make
ACPI_DEBUGGER1 input-1.mak /^CFLAGS1 ?= -DACPI_DEBUGGER1\\$/;" d language:CPreProcessor
CFLAGS2 input-1.mak /^CFLAGS2 := -DACPI_DEBUGGER2\\$/;" m language:Make
ACPI_DEBUGGER2 input-1.mak /^CFLAGS2 := -DACPI_DEBUGGER2\\$/;" d language:CPreProcessor
CFLAGS3 input-1.mak /^CFLAGS3 = -DACPI_DEBUGGER3\\$/;" m language:Make
ACPI_DEBUGGER3 input-1.mak /^CFLAGS3 = -DACPI_DEBUGGER3\\$/;" d language:CPreProcessor
FOO_$(VAL) input-2.mak /^X += -DFOO_$(VAL)$/;" d language:CPreProcessor
ARM_ARCH_SELECTOR input-2.mak /^ARM_ARCH_SELECTOR := $(filter -D__LINUX_ARM_ARCH__%, $(KBUILD_CFLAGS))$/;" m language:Make
__LINUX_ARM_ARCH__% input-2.mak /^ARM_ARCH_SELECTOR := $(filter -D__LINUX_ARM_ARCH__%, $(KBUILD_CFLAGS))$/;" d language:CPreProcessor
12 changes: 12 additions & 0 deletions Units/parser-make.r/cpp-marcros.d/input-0.mak
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Taken from linux/tools/power/acpi/tools/pfrut/Makefile
# Don't capture object starting from $.
install-man: $(srctree)/man/pfrut.8
$(ECHO) " INST " pfrut.8
$(QUIET) $(INSTALL_DATA) -D $< $(DESTDIR)$(mandir)/man8/pfrut.8

# Taken from linux/arch/loongarch/Makefile
# Don't capture object starting from -.h
install:
$(Q)install -D -m 755 $(KBUILD_IMAGE) $(INSTALL_PATH)/$(image-name-y)-$(KERNELRELEASE)
$(Q)install -D -m 644 .config $(INSTALL_PATH)/config-$(KERNELRELEASE)
$(Q)install -D -m 644 System.map $(INSTALL_PATH)/System.map-$(KERNELRELEASE)
11 changes: 11 additions & 0 deletions Units/parser-make.r/cpp-marcros.d/input-1.mak
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CFLAGS0 += -DACPI_DEBUGGER0\
-I.

CFLAGS1 ?= -DACPI_DEBUGGER1\
-I.

CFLAGS2 := -DACPI_DEBUGGER2\
-I.

CFLAGS3 = -DACPI_DEBUGGER3\
-I.
2 changes: 2 additions & 0 deletions Units/parser-make.r/cpp-marcros.d/input-2.mak
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
X += -DFOO_$(VAL)
ARM_ARCH_SELECTOR := $(filter -D__LINUX_ARM_ARCH__%, $(KBUILD_CFLAGS))
52 changes: 52 additions & 0 deletions Units/parser-make.r/cpp-marcros.d/input.mak
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Derrived from linux/Makefile

REALMODE_CFLAGS := -std=gnu11 -m16 -g -Os -DDISABLE_BRANCH_PROFILING -D__DISABLE_EXPORTS \
-Wall -Wstrict-prototypes -march=i386 -mregparm=3 \
-fno-strict-aliasing -fomit-frame-pointer -fno-pic \
-DMYDEF=1 \
-DMYDEF0 \
-DMYDEF1=1 \
-DMYDEF2(X)=X \
-D MYDEF3 \
-D MYDEF4=1 \
-D MYDEF5(X)=X \
-D'MYDEF6' \
-D'MYDEF7=1' \
-D'MYDEF8(X)=X' \
-D 'MYDEF9' \
-D 'MYDEFa=1' \
-D 'MYDEFb(X)=X' \
-D"MYDEFc" \
-D"MYDEFd=1" \
-D"MYDEFe(X)=X" \
-D "MYDEFf" \
-D "MYDEFg=1" \
-D "MYDEFh(X)=X" \
-mno-mmx -mno-sse $(call cc-option,-fcf-protection=none)

KBUILD_CPPFLAGS := -D__KERNEL__
KBUILD_RUSTFLAGS := $(rust_common_flags) \
-Cpanic=abort -Cembed-bitcode=n -Clto=n \
-Cforce-unwind-tables=n -Ccodegen-units=1 \
-Csymbol-mangling-version=v0 \
-Crelocation-model=static \
-Zfunction-sections=n \
-Dclippy::float_arithmetic

KBUILD_AFLAGS_KERNEL :=
KBUILD_CFLAGS_KERNEL :=
KBUILD_RUSTFLAGS_KERNEL :=
KBUILD_AFLAGS_MODULE := -DMODULE
KBUILD_CFLAGS_MODULE := -DMODULE
KBUILD_RUSTFLAGS_MODULE := --cfg MODULE

all:
$(CC) -DFOO ...

define macdef
MACDEF_CPPFLAGS = -DMACDEFMAC0(X0,Y0,Z0)
MACDEF_CPPFLAGS += -DMACDEFMAC1(X1,Y1,Z1)=X1##Y1##Z1
MACDEF_CPPFLAGS_BROKEN0 = -DMACDEFMAC_BROKEN0(A0,B0,C0
MACDEF_CPPFLAGS_BROKEN1 = -D MACDEFMAC_BROKEN1(A1,B1,C1
MACDEF_CPPFLAGS += -DMACDEFMAC2(X2,Y2,Z2)="f b"
endif
1 change: 1 addition & 0 deletions docs/man-pages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Man pages
ctags-lang-kconfig(7) <man/ctags-lang-kconfig.7.rst>
ctags-lang-ldscript(7) <man/ctags-lang-ldscript.7.rst>
ctags-lang-lex(7) <man/ctags-lang-lex.7.rst>
ctags-lang-make(7) <man/ctags-lang-make.7.rst>
ctags-lang-markdown(7) <man/ctags-lang-markdown.7.rst>
ctags-lang-powershell(7) <man/ctags-lang-powershell.7.rst>
ctags-lang-python(7) <man/ctags-lang-python.7.rst>
Expand Down
107 changes: 107 additions & 0 deletions docs/man/ctags-lang-make.7.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
.. _ctags-lang-make(7):

==============================================================
ctags-lang-make
==============================================================

Random notes about tagging Make source code with Universal Ctags

:Version: 6.1.0
:Manual group: Universal Ctags
:Manual section: 7

SYNOPSIS
--------
| **ctags** ... --languages=+Make ...
| **ctags** ... --language-force=Make ...
| **ctags** ... --map-Make=+([Mm]akefile) --map-Make=+(GNUmakefile) ...
| **ctags** ... --map-Make=+.mak --map-Make=+.mk ...
DESCRIPTION
-----------
This parser extracts macro and target definitions. It also extracts included files as references.

Examples
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"input.mak"

.. code-block:: Makefile
-include base.mak
SRC = hello.c
all: hello
hello: hello.o
hello.o: hello.c
$(CC) -c $(CFLAGS) $(CPPFLAGS) $<
"output.tags"
with "--options=NONE --extras=+r --fields=+KlrE -o - input.mak"

.. code-block:: tags
SRC input.mak /^SRC = hello.c$/;" macro language:Make roles:def
all input.mak /^all: hello$/;" target language:Make roles:def
base.mak input.mak /^-include base.mak$/;" makefile language:Make roles:optional extras:reference
hello input.mak /^hello: hello.o$/;" target language:Make roles:def
hello.o input.mak /^hello.o: hello.c$/;" target language:Make roles:def
EXTRACTING CPP MACRO DEFINTIONS DEFINED WITH -DFOO
---------------------------------------------------
With ``-D`` option in a C compiler like gcc, a programmer can define a
macro outside C source files. The options appears on a Makefile
frequently. For an example:

.. code-block:: Makefile
CPPFLAGS = -DDEBUG
The Make parser has heuristics [DINMAKE]_ for extracting the macros defined with
the option. With enabling ``CppDef`` extra, you can turn on the heuristics.

Examples
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

"input.mak"

.. code-block:: Makefile
-include base.mak
CFLAGS = -g -O2
CPPFLAGS = -DOUTPUT=stdout
SRC = hello.c
all: hello
hello: hello.o
hello.o: hello.c
$(CC) -c $(CFLAGS) $(CPPFLAGS) $<
"output.tags"
with "--options=NONE --extras-Make=+{CppDef} --fields=+KlE -o - input.mak"

.. code-block:: tags
CFLAGS input.mak /^CFLAGS = -g -O2$/;" macro language:Make
CPPFLAGS input.mak /^CPPFLAGS = -DOUTPUT=stdout$/;" macro language:Make
OUTPUT input.mak /^CPPFLAGS = -DOUTPUT=stdout$/;" macro language:CPreProcessor extras:CppDef
SRC input.mak /^SRC = hello.c$/;" macro language:Make
all input.mak /^all: hello$/;" target language:Make
hello input.mak /^hello: hello.o$/;" target language:Make
hello.o input.mak /^hello.o: hello.c$/;" target language:Make
VERSIONS
--------

Change since "0.0"
~~~~~~~~~~~~~~~~~~

* New extra ``CppDef`` [DINMAKE]_

SEE ALSO
--------
:ref:`ctags(1) <ctags(1)>`

.. [DINMAKE] `CONFIG_X86_X32_ABI is not visible (defined in Makefile, not C or Kconfig) <https://github.com/bootlin/elixir/issues/221>`_ (https://github.com/bootlin/elixir/issues/221)
2 changes: 1 addition & 1 deletion main/entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ extern void resetTagCorkState (tagEntryInfo *const tag,
* If your parser called one of initTagEntry () family but didn't call
* makeTagEntry () for a tagEntry yet, use attachParserField ().
*
* The parser (== caller) keeps the memory object specified with `value'
* The parser (== caller) must keep the memory object specified with `value'
* till calling makeTagEntry (). The parser must free the memory object
* after calling makeTagEntry () if it is allocated dynamically in the
* parser side.
Expand Down
1 change: 1 addition & 0 deletions man/GNUmakefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ GEN_IN_MAN_FILES = \
ctags-lang-kconfig.7 \
ctags-lang-ldscript.7 \
ctags-lang-lex.7 \
ctags-lang-make.7 \
ctags-lang-markdown.7 \
ctags-lang-powershell.7 \
ctags-lang-python.7 \
Expand Down
Loading

0 comments on commit e79e67d

Please sign in to comment.