115 Commits

Author SHA1 Message Date
Michael Kubacki
ba8010bc6c BaseTools/Ecc: Apply CParser4 ANTLR 4.9 regeneration whitespace changes
Whitespace-only changes produced by the ANTLR 4.9 code generator.

Signed-off-by: Michael Kubacki <michael.kubacki@microsoft.com>
2026-02-27 23:18:15 +00:00
Michael Kubacki
ae24028512 BaseTools/Ecc: Regenerate CParser4 files with ANTLR 4.9
The CParser4 Python parser files (CLexer.py, CParser.py, CListener.py)
were generated 7 years ago with ANTLR 4.7.1.

Meanwhile, pip-requirements.txt pins antlr4-python3-runtime to version
4.9 in commit 4a7dd50, but the files were patched, not fully
regenerated. This version mismatch produced two failures when running
EccMain.py against non-trivial C code:

1. A runtime warning on every file parsed:
   "ANTLR runtime and generated code versions disagree: 4.9!=4.7.1"

2. A crash when parsing complex C constructs that exercise the
   struct/union definition rule in CParser.py:

   TypeError: '<' not supported between instances of 'tuple' and 'int'

   This occurs in antlr4/BufferedTokenStream.py getText() because the
   4.9 runtime changed the expected argument types for that method,
   and the 4.7.1-generated parser was passing a tuple where an int is
   now required.

This change regenerates the CParser4 files with ANTLR 4.9 to resolve
the version mismatch.

Steps used to regenerate the files:

  1. Download the ANTLR 4.9 complete tool JAR:
     - `https://www.antlr.org/download/antlr-4.9-complete.jar`

  2. Generate Python3 parser files from the grammar:

     ```
     java -jar antlr-4.9-complete.jar `
       -Dlanguage=Python3 -visitor `
       -o BaseTools/Source/Python/Ecc/CParser4_new `
       BaseTools/Source/Python/Ecc/CParser4/C.g4
     ```

Signed-off-by: Michael Kubacki <michael.kubacki@microsoft.com>
2026-02-27 23:18:15 +00:00
Michael Kubacki
5ce48c03cb BaseTools/Ecc: Remove #ifndef include guard checks
The codebase has moved from traditional `#ifndef` include guards to
`#pragma once`. Remove the ECC checks that validated include guard
presence and naming conventions since they are no longer applicable.

The following checks are removed:

- IncludeFileCheckIfndefStatement: Verified all header file contents
  were guarded by a `#ifndef` statement, that the `#ifndef` was the
  first line of code after the file header comment, and that the
  `#endif` appeared on the last line.

- NamingConventionCheckIfndefStatement: Verified that the `#ifndef`
  guard name at the start of an include file used a postfix underscore
  and no prefix underscore character.

Also removed related error codes and configuration settings that were
specific to these checks.

Signed-off-by: Michael Kubacki <michael.kubacki@microsoft.com>
2026-02-23 21:01:28 +00:00
Oliver Smith-Denny
2ff173af12 BaseTools: Remove ARM32 Support
edk2 is dropping support for the ARM32 architecture. This
commit removes ARM32 code from BaseTools.

Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
2025-09-25 22:04:10 +00:00
Pierre Gondois
7402bd06cf BaseTools: Ecc: Remove unnecessary code
Running the vulture tool on the Ecc folder gave the following
report. Remove the unnecessary code.

- Ecc/c.py:52:
  unused function 'GetConfig' (60% confidence)
- Ecc/c.py:484:
  unused function 'GetFileModificationTimeFromDB' (60% confidence)
- Ecc/CodeFragmentCollector.py:77:
  unused attribute '__Token' (60% confidence)
- Ecc/CodeFragmentCollector.py:78:
  unused attribute '__SkippedChars' (60% confidence)
- Ecc/CodeFragmentCollector.py:109:
  unused method '__EndOfLine' (60% confidence)
- Ecc/CodeFragmentCollector.py:134:
  unused method '__UndoOneChar' (60% confidence)
- Ecc/CodeFragmentCollector.py:221:
  unused method '__InsertComma' (60% confidence)
- Ecc/CodeFragmentCollector.py:539:
  unused method 'PrintFragments' (60% confidence)
- Ecc/Database.py:81:
  unused attribute 'text_factory' (60% confidence)
- Ecc/Database.py:214:
  unused method 'UpdateIdentifierBelongsToFunction_disabled'
  (60% confidence)
- Ecc/MetaFileWorkspace/MetaDataTable.py:142:
  unused method 'IsIntegral' (60% confidence)
- Ecc/MetaFileWorkspace/MetaDataTable.py:205:
  unused method 'GetCrossIndex' (60% confidence)
- Ecc/Xml/XmlRoutines.py:145:
  unused function 'XmlElementData' (60% confidence)
- Ecc/Xml/XmlRoutines.py:162:
  unused function 'XmlElementList' (60% confidence)
- Ecc/Xml/XmlRoutines.py:192:
  unused function 'XmlNodeName' (60% confidence)

Signed-off-by: Pierre Gondois <pierre.gondois@arm.com>
2025-07-01 09:51:38 +08:00
Pierre Gondois
c169a5420b BaseTools: Remove unreachable code
Running the vulture tool gave the following report.
Remove the unreachable code.

- TargetTool/TargetTool.py:49:
  unreachable code after 'raise' (100% confidence)
- UPT/Library/UniClassObject.py:137:
  unreachable code after 'return' (100% confidence)
- UPT/Object/Parser/InfDefineObject.py:795:
  unreachable code after 'if' (100% confidence)
- Ecc/Check.py:1504:
  unreachable code after 'return' (100% confidence)

Signed-off-by: Pierre Gondois <pierre.gondois@arm.com>
2025-07-01 09:51:38 +08:00
Pierre Gondois
f54fe78d9a BaseTools: Remove unused import
Running the vulture tool gave the following report.
Remove the unused import.

- BPDG/BPDG.py:21:
  unused import 'encodings' (90% confidence)
- build/build.py:61:
  unused import 'Manager' (90% confidence)
- Ecc/CParser4/CLexer.py:4:
  unused import 'TextIO' (90% confidence)
- Ecc/CParser4/CParser.py:5:
  unused import 'TextIO' (90% confidence)
- Eot/CParser4/CLexer.py:4:
  unused import 'TextIO' (90% confidence)
- Eot/CParser4/CParser.py:5:
  unused import 'TextIO' (90% confidence)
- Eot/EotMain.py:28:
  unused import 'ConvertGuid' (90% confidence)
- GenFds/FdfParser.py:16:
  unused import 'hexdigits' (90% confidence)
- Table/TableEotReport.py:16:
  unused import 'EotToolError' (90% confidence)
- UPT/Library/CommentGenerating.py:19:
  unused import 'USAGE_ITEM_NOTIFY' (90% confidence)
- UPT/Library/ParserValidate.py:18:
  unused import 'COMPONENT_TYPE_LIST' (90% confidence)
- UPT/Library/UniClassObject.py:25:
  unused import 'CheckUTF16FileHeader' (90% confidence)

Signed-off-by: Pierre Gondois <pierre.gondois@arm.com>
2025-07-01 09:51:38 +08:00
Rebecca Cran
a78f525634 BaseTools: Update CParser4 to import TextIO from typing
In Python 3.13 typing.io no longer exists.
Instead, import TextIO from typing.

Signed-off-by: Rebecca Cran <rebecca@bsdio.com>
2025-05-26 17:09:27 -06:00
Rebecca Cran
259e1f04c6 BaseTools: Add @verbatim as allowed Doxygen tag
Per the discussion in https://github.com/tianocore/edk2/pull/6476,
update Ecc code to allow the @verbatim Doxygen tag.

Signed-off-by: Rebecca Cran <rebecca@bsdio.com>
2025-02-04 00:12:00 +00:00
Yang Gang
3ac092cf72 BaseTools: Clean up os.path.normcase and os.path.normpath usage
Refer to the docs of python, `os.path.normcase(path)` function:
"Normalize the case of a pathname. On Windows, convert all characters in
the pathname to lowercase, and also convert forward slashes to backward
slashes. On other operating systems, return the path unchanged."

`os.path.normpath(path)` also convert forward slashes to backward slashes.

So call `os.path.normcase` after `os.path.normpath` just convert path to
lowercase on Windows(only).

And Windows is case-insensitive but case-preserving.

So the usage of `os.path.normcase(os.path.normpath(path))` can be
simplified to `os.path.normpath(path)`. Then we can use case-preserving
paths rather than lowercase paths in compile_commands.json file
or build log.

But this patch continue to use `os.path.normcase`
when comparing/searching paths.

Signed-off-by: Yang Gang <yanggang@byosoft.com.cn>
2025-01-09 07:25:45 +00:00
Mike Beaton
6820004b3e BaseTools: Fix multiple 'invalid escape sequence' warnings in tests
In Python 3.12 invalid escape sequences in strings moved from
DeprecationWarning to SyntaxWarning
(ref https://docs.python.org/3/whatsnew/changelog.html#python-3-12-0-final
and search for gh-98401). In a future Python version this will become
SyntaxError.

Multiple instances of these SyntaxWarnings are currently printed when
running the BaseTools tests using Python 3.12 (though without actually
failing the affected tests).

This commit updates all lines which were causing this type of warning.

Typical examples which needed fixing are:

- "BaseTools\Source\Python" representing a path: "\S" and "\P" are invalid
escape sequences, therefore left unchanged, therefore the test works
(with a warning in Python 3.12). r"BaseTools\Source\Python" represents
the same string, but with escapes turned off completely thus no warning.

- Where '\t\s' is used as a regex pattern, then chr(9) + '\\s' is sent
to the regex parser (with a warning in Python 3.12) since '\s' is not a
valid Python escape sequence. This works correctly, though arguably for
the wrong reasons. r'\t\s' sends the same as '\\t\\s', as originally
intended and with no warning.

(Note that ' and " are not fundamentally different in Python.)

Signed-off-by: Mike Beaton <mjsbeaton@gmail.com>
2024-09-23 04:55:53 +00:00
Leif Lindholm
5366def8d0 BaseTools: drop GeneralCheckNonAscii() from ECC
The GeneralCheckNonAscii() function is a sledgehammer rejecting any file
containing any character outside of the 7-bit ASCII encoding space, as
well as the DEL character (which seems unrelated).

This conflicts with basic stuff like correctly spelling certain proper
nouns in comments (like copyright statements), or string literals (for
example in multi-language driver binding ComponentNames).

So rip it out, to be replaced by more fine-grained checks to be added as
identified and needed.

Signed-off-by: Leif Lindholm <quic_llindhol@quicinc.com>
2024-07-15 13:27:00 +00:00
Leif Lindholm
8ade6d7bd1 BaseTools: fix consistent Ecc misspelling of ASCII
Ecc concistently referred to ASCII/Ascii as ACSII/Acsii, which
bugged me to no end when trying to figure out how those tests
worked. Fix all instances.

Signed-off-by: Leif Lindholm <quic_llindhol@quicinc.com>
2024-07-15 13:27:00 +00:00
Michael Kubacki
6c061c4715 BaseTools/Ecc: Allow static as a modifier
Currently, `STATIC` is allowed as a function modifier but `static`
results in the below ECC errors:

```
*Error code: 5001
*Return type of a function should exist and in the first line
*file: D:\src\edk2\Build\.pytool\Plugin\EccCheck\MdePkg\Library\UefiDebugLibDebugPortProtocol\DebugLibConstructor.c
*Line number: 37
*[UefiDebugLibDebugPortProtocolExitBootServicesCallback] Return
 Type should appear at the start of line

EFI coding style error
*Error code: 5002
*Any optional functional modifiers should exist and next to the
 return type
*file: D:\src\edk2\Build\.pytool\Plugin\EccCheck\MdePkg\Library\UefiDebugLibDebugPortProtocol\DebugLibConstructor.c
*Line number: 37
```

This is because `GetDataTypeFromModifier()` will return both `static`
and the return type (e.g. `VOID`) whereas for a modifier in the list
(e.g. `STATIC`) it will return only the return type allowing logic in
Ecc/c.py to process the modifier and return type with current logic.

Signed-off-by: Michael Kubacki <michael.kubacki@microsoft.com>
2024-07-10 14:32:08 +00:00
Pierre Gondois
b03dceb87f BaseTools: Remove duplicated words in Python tools
In an effort to clean the documentation of the above
package, remove duplicated words.

Cc: Bob Feng <bob.c.feng@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Reviewed-by: Bob Feng <bob.c.feng@intel.com>
Signed-off-by: Pierre Gondois <pierre.gondois@arm.com>
2022-10-19 06:32:07 +00:00
Rebecca Cran
b600f253b3 BaseTools/Ecc: Fix grammar in Ecc error message
Signed-off-by: Rebecca Cran <quic_rcran@quicinc.com>
Reviewed-by: Yuwei Chen <yuwei.chen@intel.com>
Reviewed-by: Bob Feng <bob.c.feng@intel.com>
2022-06-27 01:06:48 +00:00
wenyi,xie via groups.io
ec30a4a0c3 BaseTools:Support decimal version number in ECC check
REF:https://bugzilla.tianocore.org/show_bug.cgi?id=3872

When doing ecc inf version check, the decimal type version number
like 1.27 is treated as invalid version.
So the code should be updated to support decimal type version number.

Cc: Bob Feng <bob.c.feng@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Yuwei Chen <yuwei.chen@intel.com>
Signed-off-by: Wenyi Xie <xiewenyi2@huawei.com>
Reviewed-by: Bob Feng <bob.c.feng@intel.com>
2022-03-28 01:45:05 +00:00
Chen, Christine
6ed6abd6c1 BaseTools: Change RealPath to AbsPath
Currently the realpath is used when parse modules, which shows the
path with a drive letter in build log. In Windows 'subst' comand is
used to associates a path with a drive letter, when use the mapped
drive letter for build, with realpath function the build log will
have different disk letter info which will cause confusion. In this
situation, if use adspath function to show the path info, it will keep
same letter with the mapped drive letter, which avoids confusion.
This patch modifies the realpath to abspath.

Cc: Bob Feng <bob.c.feng@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Signed-off-by: Yuwei Chen <yuwei.chen@intel.com>
Reviewed-by: Bob Feng <bob.c.feng@Intel.com>
2021-10-13 09:07:16 +00:00
Pierre Gondois
ce6c2b0016 BaseTools/Ecc: Make Ecc only check first include guard
The Ecc tool checks the format of the include guard. This check is
currently done on all the names following the '#ifndef' statement.
It should only be done on the first include guard.

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3252
Signed-off-by: Pierre Gondois <Pierre.Gondois@arm.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
Reviewed-by: Bob Feng <bob.c.feng@intel.com>
2021-03-18 02:04:18 +00:00
Pierre Gondois
6ffbb3581a BaseTools: Align include guards policy
The EDK II C Coding Standards Specification states that:
"Names starting with one or two underscores, such as
_MACRO_GUARD_FILE_NAME_H_, must not be used. They are
reserved for compiler implementation." [1]

The Ecc tool currently checks that the include guard end with
a trailing underscore. Thus, the check and the error message
should both be modified.

The new check forces having one sole trailing underscore
character, as the example in the specification shows:
"FILE_NAME_H_" [1]
This would allow to have more consistency.

[1] Section 5.3.5 "All include file contents must be protected
by a #include guard":
https://edk2-docs.gitbook.io/
edk-ii-c-coding-standards-specification/5_source_files/53_include_files

Signed-off-by: Pierre Gondois <Pierre.Gondois@arm.com>
Reviewed-by: Sami Mujawar <Sami.Mujawar@arm.com>
Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn>
2021-02-26 05:40:32 +00:00
Kun Qin
4bae4f02f3 BaseTools: Ecc/exception: Added _ModuleEntryPoint into exception list
Function '_ModuleEntryPoint' is a pre-defined interface for various EFI
module types and should not be caught violating EFI coding style. This
change added '_ModuleEntryPoint' into exception list to fix EFI coding
style error 8006 during CI build.

Cc: Bob Feng <bob.c.feng@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Yuwei Chen <yuwei.chen@intel.com>

Signed-off-by: Kun Qin <kun.q@outlook.com>
Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn>
Reviewed-by: Bob Feng <bob.c.feng@intel.com>
2021-02-01 10:01:02 -08:00
Pierre Gondois
6899e6789a BaseTools/Ecc: Allow init of function static variables
The Ecc tool currently reports the initialization of variables
at declaraton if the variable is non-constant and declared
in a function. Static variables locally defined in functions
should also be allowed to be initialized at declaration.

Signed-off-by: Pierre Gondois <Pierre.Gondois@arm.com>
Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn>
2020-12-22 01:26:10 +00:00
Sami Mujawar
b23c5b9722 BaseTools: Fix crash in ECC when parsing incorrect header
The ECC tool crashes if a C file has an incorrect file header
format.

The file ArmPkg\Library\ArmMmuLib\AArch64\ArmMmuPeiLibConstructor.c
has a file header in the incorrect format. It uses # to mark the
header comments instead of enclosing the file header in /* */. This
may have been a result of an INF file header being copied to a C
file.

A separate patch fixes the C file but ECC tool should
not crash if a file with an incorrect header is found.

Therefore, update the ECC tool to prevent it from crashing if an
incorrect file header is found. With this change the ECC tool will
report the incorrect header issue without crashing.

Signed-off-by: Sami Mujawar <sami.mujawar@arm.com>
Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn>
2020-12-22 01:26:10 +00:00
Yunhua Feng
eb520b93d2 BaseTools/Ecc: Fix ECC check MetaFile Copyright information issue
BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3011

MetaFile Copyright support below format:

#  Copyright (C) 2020 Hewlett Packard Enterprise Development LP<BR>
#  (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>

Cc: Bob Feng <bob.c.feng@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Yuwei Chen <yuwei.chen@intel.com>
Signed-off-by: Yunhua Feng <fengyunhua@byosoft.com.cn>
Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn>
2020-10-27 06:50:06 +00:00
Bob Feng
7513559926 BaseTools/Ecc: Fix an issue of path separator compatibility
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=2904

The path separator is different in Windows and Linux, the
original code does not handle this difference. This patch
is to fix this issue.

Signed-off-by: Bob Feng <bob.c.feng@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Yuwei Chen <yuwei.chen@intel.com>
Cc: Shenglei Zhang <shenglei.zhang@intel.com>
Message-Id: <20200901102315.38840-1-bob.c.feng@intel.com>
Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn>
2020-09-01 17:55:56 +00:00