File: source_location.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (100 lines) | stat: -rw-r--r-- 3,593 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef REMOTING_BASE_SOURCE_LOCATION_H_
#define REMOTING_BASE_SOURCE_LOCATION_H_

#include <memory>
#include <optional>
#include <string_view>

#include "base/location.h"
#include "mojo/public/cpp/bindings/struct_traits.h"

namespace remoting {

namespace mojom {
class SourceLocationDataView;
}  // namespace mojom

// SourceLocation represents certain information about the source code, such as
// file names, line numbers, and function names. This is similar to
// base::Location, but it has the following differences to allow it to be passed
// across the process boundary (via Mojo IPC):
//
// 1. SourceLocation has an optional backing store for the function name and
//    the file name, which base::Location doesn't have. Since we can't pass
//    addresses in the data segment across the process boundary, the backing
//    store is critical for reconstructing the SourceLocation object from
//    strings that get passed through IPC.
//    If the SourceLocation stays in the same process, the backing store will
//    not be created and the memory footprint is roughly the same as
//    base::Location.
//
// 2. SourceLocation does not store the program counter, since remoting does not
//    need this info, and in most cases we can't pass a code segment address
//    across the process boundary.
//
// SourceLocation can be implicitly converted from base::Location, so you can
// create it with `FROM_HERE`. It can't be converted back to base::Location
// because of the differences stated above.
class SourceLocation final {
 public:
  SourceLocation();
  SourceLocation(SourceLocation&&);
  SourceLocation(const SourceLocation&);

  // Allow creation of SourceLocation with FROM_HERE.
  // NOLINTNEXTLINE(google-explicit-constructor)
  SourceLocation(const base::Location& location);
  ~SourceLocation();

  SourceLocation& operator=(SourceLocation&&);
  SourceLocation& operator=(const SourceLocation&);

  bool operator==(const SourceLocation& other) const;

  // Returns true if there is source code location info. If this is false, the
  // location object is default-initialized.
  bool is_null() const { return !function_name_ || !file_name_; }

  // Will be nullptr for default initialized Location objects.
  const char* function_name() const { return function_name_; }

  // Will be nullptr for default initialized Location objects.
  const char* file_name() const { return file_name_; }

  // Will be -1 for default initialized Location objects.
  int line_number() const { return line_number_; }

  // Converts to the most user-readable form possible.
  std::string ToString() const;

  bool HasBackingStoreForTesting() const;

  static SourceLocation CreateWithBackingStoreForTesting(
      std::optional<std::string_view> function_name,
      std::optional<std::string_view> file_name,
      int line_number);

 private:
  friend struct mojo::StructTraits<remoting::mojom::SourceLocationDataView,
                                   ::remoting::SourceLocation>;

  struct BackingStore;

  void InitializeWithBackingStore(std::optional<std::string_view> function_name,
                                  std::optional<std::string_view> file_name,
                                  int line_number);

  const char* function_name_ = nullptr;
  const char* file_name_ = nullptr;
  int line_number_ = -1;

  std::unique_ptr<BackingStore> backing_store_;
};

}  // namespace remoting

#endif  // REMOTING_BASE_SOURCE_LOCATION_H_