-
-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[skip-changelog] legacy: Arduino preprocess subroutine refactorizatio…
…n (part 1) (#2186) * legacy: Removed ReadFileAndStoreInContext command * ctags parser.Parse signature change * removing ctags-related data from builder ctx (part 1) * removing ctags-related data from builder ctx (part 2) * removing ctags-related data from builder ctx (part 3) * Clearly separate Source code processing phases * Removed Command wrapper from ContainerMergeCopySketchFiles * Moved builder.CopySketchFilesToBuildPath into proper location * Converted FilterSketchSource into a function * Moved a couple of functions in the proper builder package * Fixed lint error
- Loading branch information
Showing
27 changed files
with
507 additions
and
477 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2023 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This software is released under the GNU General Public License version 3, | ||
// which covers the main part of arduino-cli. | ||
// The terms of this license can be found at: | ||
// https://www.gnu.org/licenses/gpl-3.0.en.html | ||
// | ||
// You can be released from the requirements of the above licenses by purchasing | ||
// a commercial license. Buying such a license is mandatory if you want to | ||
// modify or otherwise use the software for commercial activities involving the | ||
// Arduino software without disclosing the source code of your own applications. | ||
// To purchase a commercial license, send an email to license@arduino.cc. | ||
|
||
package builder | ||
|
||
import ( | ||
"strings" | ||
"unicode/utf8" | ||
) | ||
|
||
// QuoteCppString returns the given string as a quoted string for use with the C | ||
// preprocessor. This adds double quotes around it and escapes any | ||
// double quotes and backslashes in the string. | ||
func QuoteCppString(str string) string { | ||
str = strings.Replace(str, "\\", "\\\\", -1) | ||
str = strings.Replace(str, "\"", "\\\"", -1) | ||
return "\"" + str + "\"" | ||
} | ||
|
||
// ParseCppString parse a C-preprocessor string as emitted by the preprocessor. This | ||
// is a string contained in double quotes, with any backslashes or | ||
// quotes escaped with a backslash. If a valid string was present at the | ||
// start of the given line, returns the unquoted string contents, the | ||
// remainder of the line (everything after the closing "), and true. | ||
// Otherwise, returns the empty string, the entire line and false. | ||
func ParseCppString(line string) (string, string, bool) { | ||
// For details about how these strings are output by gcc, see: | ||
// https://github.com/gcc-mirror/gcc/blob/a588355ab948cf551bc9d2b89f18e5ae5140f52c/libcpp/macro.c#L491-L511 | ||
// Note that the documentation suggests all non-printable | ||
// characters are also escaped, but the implementation does not | ||
// actually do this. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51259 | ||
if len(line) < 1 || line[0] != '"' { | ||
return "", line, false | ||
} | ||
|
||
i := 1 | ||
res := "" | ||
for { | ||
if i >= len(line) { | ||
return "", line, false | ||
} | ||
|
||
c, width := utf8.DecodeRuneInString(line[i:]) | ||
|
||
switch c { | ||
case '\\': | ||
// Backslash, next character is used unmodified | ||
i += width | ||
if i >= len(line) { | ||
return "", line, false | ||
} | ||
res += string(line[i]) | ||
case '"': | ||
// Quote, end of string | ||
return res, line[i+width:], true | ||
default: | ||
res += string(c) | ||
} | ||
|
||
i += width | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package builder_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/arduino/arduino-cli/arduino/builder" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestParseCppString(t *testing.T) { | ||
_, _, ok := builder.ParseCppString(`foo`) | ||
require.Equal(t, false, ok) | ||
|
||
_, _, ok = builder.ParseCppString(`"foo`) | ||
require.Equal(t, false, ok) | ||
|
||
str, rest, ok := builder.ParseCppString(`"foo"`) | ||
require.Equal(t, true, ok) | ||
require.Equal(t, `foo`, str) | ||
require.Equal(t, ``, rest) | ||
|
||
str, rest, ok = builder.ParseCppString(`"foo\\bar"`) | ||
require.Equal(t, true, ok) | ||
require.Equal(t, `foo\bar`, str) | ||
require.Equal(t, ``, rest) | ||
|
||
str, rest, ok = builder.ParseCppString(`"foo \"is\" quoted and \\\\bar\"\" escaped\\" and "then" some`) | ||
require.Equal(t, true, ok) | ||
require.Equal(t, `foo "is" quoted and \\bar"" escaped\`, str) | ||
require.Equal(t, ` and "then" some`, rest) | ||
|
||
str, rest, ok = builder.ParseCppString(`" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_abcdefghijklmnopqrstuvwxyz{|}~"`) | ||
require.Equal(t, true, ok) | ||
require.Equal(t, ` !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_abcdefghijklmnopqrstuvwxyz{|}~`, str) | ||
require.Equal(t, ``, rest) | ||
|
||
str, rest, ok = builder.ParseCppString(`"/home/ççç/"`) | ||
require.Equal(t, true, ok) | ||
require.Equal(t, `/home/ççç/`, str) | ||
require.Equal(t, ``, rest) | ||
|
||
str, rest, ok = builder.ParseCppString(`"/home/ççç/ /$sdsdd\\"`) | ||
require.Equal(t, true, ok) | ||
require.Equal(t, `/home/ççç/ /$sdsdd\`, str) | ||
require.Equal(t, ``, rest) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.