@@ -178,17 +178,34 @@ namespace pl::core {
178178 }
179179
180180 if (m_defines.contains (name)) {
181- bool isValueSame = m_defines[name] == values;
181+ bool isValueSame = m_defines[name]. values == values;
182182 if (!isValueSame) {
183- errorAt (values[0 ].location , " Previous definition occurs at line '{}'." , m_defines[name][0 ].location .line );
184- errorAt (m_defines[name][0 ].location , " Macro '{}' is redefined in line '{}'." , name, values[0 ].location .line );
185- m_defines[name].clear ();
186- m_defines[name] = values;
183+ auto & define = m_defines[name];
184+ Location defineLocation{};
185+
186+ if (define.values .empty ()) {
187+ defineLocation = define.nameToken .location ;
188+ } else {
189+ defineLocation = define.values [0 ].location ;
190+ }
191+
192+ Location ourLocation{};
193+ if (values.empty ()) {
194+ ourLocation = token.location ;
195+ } else {
196+ ourLocation = values[0 ].location ;
197+ }
198+
199+ errorAt (ourLocation, " Previous definition occurs at line '{}'." , defineLocation.line );
200+ errorAt (defineLocation, " Macro '{}' is redefined in line '{}'." , name, defineLocation.line );
201+
202+ m_defines[name] = { token, values };
203+
187204 removeKey (token);
188205 m_keys.emplace_back (token);
189206 }
190207 } else {
191- m_defines[name] = values;
208+ m_defines[name] = { token, values } ;
192209 m_keys.emplace_back (token);
193210 }
194211 }
@@ -340,7 +357,7 @@ namespace pl::core {
340357 if (auto *tokenLiteral = std::get_if<Token::Literal>(&m_token->value ); m_token->type == Token::Type::String && tokenLiteral != nullptr ) {
341358 path = tokenLiteral->toString (false );
342359
343- } else if (auto *identifier = std::get_if<Token::Identifier>(&m_token->value ); m_token->type == Token::Type::Identifier) {
360+ } else if (auto *identifier = std::get_if<Token::Identifier>(&m_token->value ); m_token->type == Token::Type::Identifier && identifier != nullptr ) {
344361 saveImport.push_back (*m_token);
345362 path = identifier->get ();
346363 m_token++;
@@ -476,7 +493,7 @@ namespace pl::core {
476493 Token::Identifier *tokenIdentifier = std::get_if<Token::Identifier>(&m_token->value );
477494 if (tokenIdentifier != nullptr )
478495 tokenIdentifier->setType (Token::Identifier::IdentifierType::Macro);
479- for (const auto &newToken: m_defines[keyIdentifier->get ()])
496+ for (const auto &newToken: m_defines[keyIdentifier->get ()]. values )
480497 resultValues.push_back (newToken);
481498 } else
482499 resultValues.push_back (value);
@@ -584,7 +601,7 @@ namespace pl::core {
584601 this ->error (item);
585602 return { m_output, collectErrors () };
586603 }
587-
604+ setLongestLineLength (lexer-> getLongestLineLength ());
588605 m_token = m_result.begin ();
589606 m_initialized = true ;
590607 while (!eof ())
@@ -614,7 +631,9 @@ namespace pl::core {
614631 }
615632
616633 void Preprocessor::addDefine (const std::string &name, const std::string &value) {
617- m_defines[name] = {Token { Token::Type::String, value, {nullptr , 0 , 0 , 0 } } } ;
634+ auto nameToken = Token { Token::Type::Identifier, name, Location::Empty () };
635+ auto valueToken = Token { Token::Type::String, value, Location::Empty () };
636+ m_defines[name] = { nameToken, { valueToken }};
618637 }
619638
620639 void Preprocessor::addPragmaHandler (const std::string &pragmaType, const api::PragmaHandler &handler) {
0 commit comments