Skip to content
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

[clang] Update the lifetimebound example with up-to-date expected warning and change the sample code to be a fully working example #113437

Merged
merged 1 commit into from
Oct 30, 2024

Conversation

bricknerb
Copy link
Contributor

@bricknerb bricknerb commented Oct 23, 2024

Tested the code: https://godbolt.org/z/n5xcq65YM
Tested the generated documentation: BruDQ2UkTXHA9PE

…ning and change the sample code to be a fully working example.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 23, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 23, 2024

@llvm/pr-subscribers-clang

Author: Boaz Brickner (bricknerb)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/113437.diff

1 Files Affected:

  • (modified) clang/include/clang/Basic/AttrDocs.td (+20-8)
diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td
index ee8126cadae232..c2552d459cb474 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -3702,20 +3702,32 @@ user-declared functions. For example:
 
 .. code-block:: c++
 
+    #include <map>
+    #include <string>
+
+    using namespace std::literals;
+
     // Returns m[key] if key is present, or default_value if not.
     template<typename T, typename U>
     const U &get_or_default(const std::map<T, U> &m [[clang::lifetimebound]],
                             const T &key, /* note, not lifetimebound */
-                            const U &default_value [[clang::lifetimebound]]);
+                            const U &default_value [[clang::lifetimebound]]) {
+      if (auto iter = m.find(key); iter != m.end()) return iter->second;
+      else return default_value;
+    }
 
-    std::map<std::string, std::string> m;
-    // warning: temporary "bar"s that might be bound to local reference 'val'
-    // will be destroyed at the end of the full-expression
-    const std::string &val = get_or_default(m, "foo"s, "bar"s);
+    int main() {
+      std::map<std::string, std::string> m;
+      // warning: temporary bound to local reference 'val1' will be destroyed
+      // at the end of the full-expression
+      const std::string &val1 = get_or_default(m, "foo"s, "bar"s);
 
-    // No warning in this case.
-    std::string def_val = "bar"s;
-    const std::string &val = get_or_default(m, "foo"s, def_val);
+      // No warning in this case.
+      std::string def_val = "bar"s;
+      const std::string &val2 = get_or_default(m, "foo"s, def_val);
+
+      return 0;
+    }
 
 The attribute can be applied to the implicit ``this`` parameter of a member
 function by writing the attribute after the function type:

@bricknerb bricknerb marked this pull request as draft October 23, 2024 10:08
@bricknerb bricknerb marked this pull request as ready for review October 23, 2024 12:07
@bricknerb
Copy link
Contributor Author

The Test documentation build failure seems unrelated since it refers to ClangCommandLineReference.

@cor3ntin cor3ntin requested a review from shafik October 23, 2024 12:16
Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, other than the minor comment.

clang/include/clang/Basic/AttrDocs.td Show resolved Hide resolved
@bricknerb bricknerb merged commit 9c8dab0 into llvm:main Oct 30, 2024
13 of 14 checks passed
NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this pull request Nov 4, 2024
…ning and change the sample code to be a fully working example (llvm#113437)

Tested the code: https://godbolt.org/z/n5xcq65YM
Tested the generated documentation:
![BruDQ2UkTXHA9PE](https://github.com/user-attachments/assets/cf527d1a-ef3b-41f2-84c2-4ca38af16d2d)
@bricknerb bricknerb deleted the lifetime branch November 7, 2024 11:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants