-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for doc comments in icrate generation #435
Draft
simlay
wants to merge
3
commits into
madsmtm:master
Choose a base branch
from
simlay:add-doc-comments-to-header-translator
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,126 @@ | ||
//! Utilities for manipulating C/C++ comments. | ||
|
||
/// The type of a comment. | ||
#[derive(Debug, PartialEq, Eq)] | ||
enum Kind { | ||
/// A `///` comment, or something of the like. | ||
/// All lines in a comment should start with the same symbol. | ||
SingleLines, | ||
/// A `/**` comment, where each other line can start with `*` and the | ||
/// entire block ends with `*/`. | ||
MultiLine, | ||
} | ||
|
||
/// Preprocesses a C/C++ comment so that it is a valid Rust comment. | ||
pub(crate) fn preprocess(comment: &str) -> String { | ||
match self::kind(comment) { | ||
Some(Kind::SingleLines) => preprocess_single_lines(comment), | ||
Some(Kind::MultiLine) => preprocess_multi_line(comment), | ||
None => comment.to_owned(), | ||
} | ||
} | ||
|
||
/// Gets the kind of the doc comment, if it is one. | ||
fn kind(comment: &str) -> Option<Kind> { | ||
if comment.starts_with("/*") { | ||
Some(Kind::MultiLine) | ||
} else if comment.starts_with("//") { | ||
Some(Kind::SingleLines) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
/// Preprocesses multiple single line comments. | ||
/// | ||
/// Handles lines starting with both `//` and `///`. | ||
fn preprocess_single_lines(comment: &str) -> String { | ||
debug_assert!(comment.starts_with("//"), "comment is not single line"); | ||
|
||
let lines: Vec<_> = comment | ||
.lines() | ||
.map(|l| { | ||
l.trim() | ||
.trim_end_matches('/') | ||
.trim_end_matches('*') | ||
.trim_start_matches('/') | ||
}) | ||
.collect(); | ||
lines.join("\n") | ||
} | ||
|
||
fn preprocess_multi_line(comment: &str) -> String { | ||
let comment = comment | ||
.trim_start_matches('/') | ||
.trim_end_matches('/') | ||
.trim_end_matches('*'); | ||
|
||
// Strip any potential `*` characters preceding each line. | ||
let mut lines: Vec<_> = comment | ||
.lines() | ||
.map(|line| { | ||
line.trim_start_matches('/') | ||
.trim_end_matches('/') | ||
.trim_end_matches('*') | ||
.trim() | ||
.trim_start_matches('*') | ||
.trim_start_matches('!') | ||
}) | ||
.skip_while(|line| line.trim().is_empty()) // Skip the first empty lines. | ||
.collect(); | ||
|
||
// Remove the trailing line corresponding to the `*/`. | ||
if lines.last().map_or(false, |l| l.trim().is_empty()) { | ||
lines.pop(); | ||
} | ||
|
||
lines.join("\n") | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn picks_up_single_and_multi_line_doc_comments() { | ||
assert_eq!(kind("/// hello"), Some(Kind::SingleLines)); | ||
assert_eq!(kind("/** world */"), Some(Kind::MultiLine)); | ||
} | ||
|
||
#[test] | ||
fn processes_single_lines_correctly() { | ||
assert_eq!(preprocess("///"), ""); | ||
assert_eq!(preprocess("/// hello"), " hello"); | ||
assert_eq!(preprocess("// hello"), " hello"); | ||
assert_eq!(preprocess("// hello"), " hello"); | ||
} | ||
|
||
#[test] | ||
fn processes_multi_lines_correctly() { | ||
assert_eq!(preprocess("/**/"), ""); | ||
|
||
assert_eq!( | ||
preprocess("/** hello \n * world \n * foo \n */"), | ||
" hello\n world\n foo" | ||
); | ||
|
||
assert_eq!( | ||
preprocess("/**\nhello\n*world\n*foo\n*/"), | ||
"hello\nworld\nfoo" | ||
); | ||
assert_eq!( | ||
preprocess( | ||
r#"/************************ Deprecated ************************/ | ||
// Following NSStringDrawing methods are soft deprecated starting with OS X 10.11. It will be officially deprecated in a future release. Use corresponding API with NSStringDrawingContext instead"# | ||
), | ||
r#" Deprecated | ||
Following NSStringDrawing methods are soft deprecated starting with OS X 10.11. It will be officially deprecated in a future release. Use corresponding API with NSStringDrawingContext instead"# | ||
); | ||
assert_eq!( | ||
preprocess( | ||
r#"/// The \c contentLayoutRect will return the area inside the window that is for non-obscured content. Typically, this is the same thing as the `contentView`'s frame. However, for windows with the \c NSFullSizeContentViewWindowMask set, there needs to be a way to determine the portion that is not under the toolbar. The \c contentLayoutRect returns the portion of the layout that is not obscured under the toolbar. \c contentLayoutRect is in window coordinates. It is KVO compliant. */"# | ||
), | ||
r#" The \c contentLayoutRect will return the area inside the window that is for non-obscured content. Typically, this is the same thing as the `contentView`'s frame. However, for windows with the \c NSFullSizeContentViewWindowMask set, there needs to be a way to determine the portion that is not under the toolbar. The \c contentLayoutRect returns the portion of the layout that is not obscured under the toolbar. \c contentLayoutRect is in window coordinates. It is KVO compliant. "# | ||
); | ||
} | ||
} |
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
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied this file straight out of bindgen and then modified it for some of the extra gross objective-c comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you leave attribution in the file? I think that should alleviate most licensing issues (they have a different license than us), but IANAL.