ClangSharp icon indicating copy to clipboard operation
ClangSharp copied to clipboard

`#pragma once` doesn't seem to be respected

Open ds5678 opened this issue 1 month ago • 2 comments

I'm working on bindings for LibTorch, and one of the issues I found while using ClangSharp on library.h is with #pragma once.

./include/torch/library.h:93:12: error: redefinition of '_RegisterOrVerify' ./include/torch/library.h:107:17: error: redefinition of 'CppFunction' 
./include/torch/library.h:340:20: error: redefinition of 'dispatch' ./include/torch/library.h:354:20: error: redefinition of 'dispatch' 
./include/torch/library.h:412:83: error: redefinition of default argument ./include/torch/library.h:421:55: error: redefinition of default argument 
./include/torch/library.h:431:30: error: redefinition of 'schema' ./include/torch/library.h:437:61: error: redefinition of 'constructSchemaOrName' 
./include/torch/library.h:441:61: error: redefinition of 'constructSchemaOrName' ./include/torch/library.h:446:1: error: redefinition of 'constructSchemaOrName' 
./include/torch/library.h:482:7: error: redefinition of 'ClassNotSelected' ./include/torch/library.h:499:7: error: redefinition of 'SelectiveStr' 
./include/torch/library.h:543:17: error: redefinition of 'Library' ./include/torch/library.h:934:7: error: redefinition of 'TorchLibraryInit'

It generates fine if I replace #pragma once with this traditional pattern:

#ifndef TORCH_LIBRARY_123_
#define TORCH_LIBRARY_123_

// Code

#endif // TORCH_LIBRARY_123_

As with any issue I post, I'm willing to make a pull request. However, I'm not sure where to start with this one and would appreciate some guidance.

ds5678 avatar Oct 28 '25 20:10 ds5678

My workaround for solving this was to preprocess all files with #pragma once like this:

#ifndef PRAGMA_ONCE_15255042718534948365
#define PRAGMA_ONCE_15255042718534948365

// File contents

#endif // PRAGMA_ONCE_15255042718534948365

ds5678 avatar Oct 29 '25 05:10 ds5678

ClangSharp simply passes the header down to Clang. Any support for features like #pragma once (which is non-standard) would be dependent on the options passed into Clang.

ClangSharp by default doesn't really pass down anything additional, which allows the user full flexibility and the tool defaults. The user can specify additional options to pass down using --additional ..., noting there is built-in support for the common cases of --language (short alias -x), --std (short alias -std), --include-directory (short alias -I), and --define-macro (short alias -D)

Now I'd expect that to just work by default, but Clang is sometimes weird with their support and why things do or do not work as expected.

tannergooding avatar Nov 01 '25 15:11 tannergooding